From c6df13cc853d7fa2b614171de5e23db7ad2154d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADcio=20Godoy?= Date: Sun, 31 Mar 2024 11:03:30 -0300 Subject: [PATCH 1/2] Support chaining using Which property --- Directory.Build.props | 4 ++-- ...pFunctionalExtensions.FluentAssertions.csproj | 1 + .../MaybeExtensions.cs | 8 ++++---- .../ResultExtensions.cs | 8 ++++---- .../ResultTEExtensions.cs | 16 ++++++++-------- .../ResultTExtensions.cs | 16 ++++++++-------- .../UnitResultExtensions.cs | 8 ++++---- .../MaybeAssertionsTests.cs | 2 ++ .../ResultAssertionTests.cs | 3 +++ .../ResultTAssertionsTests.cs | 4 ++++ .../ResultTEAssertionTests.cs | 4 ++++ .../UnitResultAssertionTests.cs | 2 ++ 12 files changed, 46 insertions(+), 30 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 237e547..6dd42ce 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -18,8 +18,8 @@ - - + + diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/CSharpFunctionalExtensions.FluentAssertions.csproj b/src/CSharpFunctionalExtensions.FluentAssertions/CSharpFunctionalExtensions.FluentAssertions.csproj index 5f8c10c..8984388 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/CSharpFunctionalExtensions.FluentAssertions.csproj +++ b/src/CSharpFunctionalExtensions.FluentAssertions/CSharpFunctionalExtensions.FluentAssertions.csproj @@ -10,6 +10,7 @@ CSharpFunctionalExtensions.FluentAssertions $(Product) $(PackageTags) + FluentAssertions diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/MaybeExtensions.cs b/src/CSharpFunctionalExtensions.FluentAssertions/MaybeExtensions.cs index a53b6cc..4e57dd2 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/MaybeExtensions.cs +++ b/src/CSharpFunctionalExtensions.FluentAssertions/MaybeExtensions.cs @@ -21,7 +21,7 @@ public MaybeAssertions(Maybe instance) : base(instance) { } /// /// /// - public AndConstraint> HaveSomeValue(string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> HaveSomeValue(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -29,7 +29,7 @@ public AndConstraint> HaveSomeValue(string because = "", para .ForCondition(v => v.HasValue) .FailWith("Expected a value {reason}"); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// @@ -39,7 +39,7 @@ public AndConstraint> HaveSomeValue(string because = "", para /// /// /// - public AndConstraint> HaveValue(T value, string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> HaveValue(T value, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -56,7 +56,7 @@ public AndConstraint> HaveValue(T value, string because = "", _ => value, v => v); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/ResultExtensions.cs b/src/CSharpFunctionalExtensions.FluentAssertions/ResultExtensions.cs index 4abe633..3e8610b 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/ResultExtensions.cs +++ b/src/CSharpFunctionalExtensions.FluentAssertions/ResultExtensions.cs @@ -37,14 +37,14 @@ public AndConstraint Succeed(string because = "", params objec /// /// /// - public AndConstraint Fail(string because = "", params object[] becauseArgs) + public AndWhichConstraint Fail(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsFailure) .FailWith(() => new FailReason($"Expected {{context:result}} to fail{{reason}}, but it succeeded")); - return new AndConstraint(this); + return new AndWhichConstraint(this, Subject.Error); } /// @@ -54,7 +54,7 @@ public AndConstraint Fail(string because = "", params object[] /// /// /// - public AndConstraint FailWith(string error, string because = "", params object[] becauseArgs) + public AndWhichConstraint FailWith(string error, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -62,6 +62,6 @@ public AndConstraint FailWith(string error, string because = " .ForCondition(b => Subject.Error!.Equals(error)) .FailWith($"Expected {{context:result}} error to be {{0}}, but found {{1}}", error, Subject.Error); - return new AndConstraint(this); + return new AndWhichConstraint(this, Subject.Error); } } diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/ResultTEExtensions.cs b/src/CSharpFunctionalExtensions.FluentAssertions/ResultTEExtensions.cs index 62ecd26..480101a 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/ResultTEExtensions.cs +++ b/src/CSharpFunctionalExtensions.FluentAssertions/ResultTEExtensions.cs @@ -20,14 +20,14 @@ public ResultTEAssertions(Result instance) : base(instance) { } /// /// /// - public AndConstraint> Succeed(string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> Succeed(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsSuccess) .FailWith(() => new FailReason(@$"Expected {{context:result}} to succeed{{reason}}, but it failed with error ""{Subject.Error}""")); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// @@ -37,7 +37,7 @@ public AndConstraint> Succeed(string because = "", para /// /// /// - public AndConstraint> SucceedWith(T value, string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> SucceedWith(T value, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -48,7 +48,7 @@ public AndConstraint> SucceedWith(T value, string becau .ForCondition(v => v!.Equals(value)) .FailWith("Expected {context:result} value to be {0}, but found {1}", value, Subject.Value); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// @@ -57,14 +57,14 @@ public AndConstraint> SucceedWith(T value, string becau /// /// /// - public AndConstraint> Fail(string because = "", params object[] becauseArgs) + public AndWhichConstraint, E> Fail(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsFailure) .FailWith(() => new FailReason(@$"Expected {{context:result}} to fail, but it succeeded with value ""{Subject.Value}""")); - return new AndConstraint>(this); + return new AndWhichConstraint, E>(this, Subject.Error); } /// @@ -74,7 +74,7 @@ public AndConstraint> Fail(string because = "", params /// /// /// - public AndConstraint> FailWith(E error, string because = "", params object[] becauseArgs) + public AndWhichConstraint, E> FailWith(E error, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -85,6 +85,6 @@ public AndConstraint> FailWith(E error, string because .ForCondition(e => e!.Equals(error)) .FailWith($"Expected {{context:result}} error to be {{0}}, but found {{1}}", error, Subject.Error); - return new AndConstraint>(this); + return new AndWhichConstraint, E>(this, Subject.Error); } } diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/ResultTExtensions.cs b/src/CSharpFunctionalExtensions.FluentAssertions/ResultTExtensions.cs index f354d85..1ce358f 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/ResultTExtensions.cs +++ b/src/CSharpFunctionalExtensions.FluentAssertions/ResultTExtensions.cs @@ -21,14 +21,14 @@ public ResultTAssertions(Result instance) : base(instance) { } /// /// /// - public AndConstraint> Succeed(string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> Succeed(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsSuccess) .FailWith(() => new FailReason(@$"Expected {{context:result}} to succeed{{reason}}, but it failed with error ""{Subject.Error}""")); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// @@ -38,7 +38,7 @@ public AndConstraint> Succeed(string because = "", params o /// /// /// - public AndConstraint> SucceedWith(T value, string because = "", params object[] becauseArgs) + public AndWhichConstraint, T> SucceedWith(T value, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -49,7 +49,7 @@ public AndConstraint> SucceedWith(T value, string because = .ForCondition(v => v!.Equals(value)) .FailWith($"Expected {{context:result}} value to be {{0}}, but found {{1}}", value, Subject.Value); - return new AndConstraint>(this); + return new AndWhichConstraint, T>(this, Subject.Value); } /// @@ -58,14 +58,14 @@ public AndConstraint> SucceedWith(T value, string because = /// /// /// - public AndConstraint> Fail(string because = "", params object[] becauseArgs) + public AndWhichConstraint, string> Fail(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsFailure) .FailWith(() => new FailReason(@$"Expected {{context:result}} to fail, but it succeeded with value ""{Subject.Value}""")); - return new AndConstraint>(this); + return new AndWhichConstraint, string>(this, Subject.Error); } /// @@ -75,7 +75,7 @@ public AndConstraint> Fail(string because = "", params obje /// /// /// - public AndConstraint> FailWith(string error, string because = "", params object[] becauseArgs) + public AndWhichConstraint, string> FailWith(string error, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -83,6 +83,6 @@ public AndConstraint> FailWith(string error, string because .ForCondition(b => Subject.Error!.Equals(error)) .FailWith($"Expected {{context:result}} error to be {{0}}, but found {{1}}", error, Subject.Error); - return new AndConstraint>(this); + return new AndWhichConstraint, string>(this, Subject.Error); } } diff --git a/src/CSharpFunctionalExtensions.FluentAssertions/UnitResultExtensions.cs b/src/CSharpFunctionalExtensions.FluentAssertions/UnitResultExtensions.cs index 72d802e..bf0c3f2 100644 --- a/src/CSharpFunctionalExtensions.FluentAssertions/UnitResultExtensions.cs +++ b/src/CSharpFunctionalExtensions.FluentAssertions/UnitResultExtensions.cs @@ -36,14 +36,14 @@ public AndConstraint> Succeed(string because = "", param /// /// /// - public AndConstraint> Fail(string because = "", params object[] becauseArgs) + public AndWhichConstraint, E> Fail(string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) .ForCondition(Subject.IsFailure) .FailWith(() => new FailReason($"Expected {{context:result}} to fail, but it succeeded")); - return new AndConstraint>(this); + return new AndWhichConstraint, E>(this, Subject.Error); } /// @@ -53,7 +53,7 @@ public AndConstraint> Fail(string because = "", params o /// /// /// - public AndConstraint> FailWith(E error, string because = "", params object[] becauseArgs) + public AndWhichConstraint, E> FailWith(E error, string because = "", params object[] becauseArgs) { Execute.Assertion .BecauseOf(because, becauseArgs) @@ -64,6 +64,6 @@ public AndConstraint> FailWith(E error, string because = .ForCondition(e => e!.Equals(error)) .FailWith($"Expected {{context:result}} error to be {{0}}, but found {{1}}", error, Subject.Error); - return new AndConstraint>(this); + return new AndWhichConstraint, E>(this, Subject.Error); } } diff --git a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/MaybeAssertionsTests.cs b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/MaybeAssertionsTests.cs index 0faa5a9..1799ae9 100644 --- a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/MaybeAssertionsTests.cs +++ b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/MaybeAssertionsTests.cs @@ -12,6 +12,7 @@ public void WhenMaybeIsExpectedToHaveSomeValueAndItDoesShouldNotThrow() var maybe = Maybe.From("test"); maybe.Should().HaveSomeValue(); + maybe.Should().HaveSomeValue().Which.Should().Be("test"); } [Fact] @@ -20,6 +21,7 @@ public void WhenMaybeIsExpectedToHaveValueAndItDoesShouldNotThrow() var maybe = Maybe.From("test"); maybe.Should().HaveValue("test"); + maybe.Should().HaveValue("test").Which.Should().HaveLength(4); } [Fact] diff --git a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultAssertionTests.cs b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultAssertionTests.cs index 2dde4ae..cedcf97 100644 --- a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultAssertionTests.cs +++ b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultAssertionTests.cs @@ -32,6 +32,9 @@ public void WhenResultIsExpectedToHaveErrorFailShouldNotThrow() result.Should().Fail(); result.Should().FailWith(error); + + result.Should().Fail().Which.Should().Be(error); + result.Should().FailWith(error).Which.Should().Be(error); } [Fact] diff --git a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTAssertionsTests.cs b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTAssertionsTests.cs index e001c79..97bc63f 100644 --- a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTAssertionsTests.cs +++ b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTAssertionsTests.cs @@ -12,6 +12,7 @@ public void WhenResultIsExpectedToHaveValueItShouldBeSuccessful() var result = Result.Success("test"); result.Should().Succeed(); + result.Should().Succeed().Which.Should().Be("test"); } [Fact] @@ -31,6 +32,7 @@ public void WhenResultIsExpectedToHaveValueItShouldBeSuccessfulWithValue() var result = Result.Success(expected); result.Should().SucceedWith(expected); + result.Should().SucceedWith(expected).Which.Should().HaveLength(4); } [Fact] @@ -51,6 +53,8 @@ public void WhenResultIsExpectedToHaveErrorFailShouldNotThrow() result.Should().Fail(); result.Should().FailWith(error); + result.Should().Fail().Which.Should().Be(error); + result.Should().FailWith(error).Which.Should().HaveLength(5); } [Fact] diff --git a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTEAssertionTests.cs b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTEAssertionTests.cs index c0fc4d4..19336da 100644 --- a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTEAssertionTests.cs +++ b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/ResultTEAssertionTests.cs @@ -15,6 +15,7 @@ public void WhenResultIsExpectedToBeSuccessItShouldBeSuccess() var action = () => result.Should().Succeed(); action.Should().NotThrow(); + result.Should().Succeed().Which.Should().Be(value); } [Fact] @@ -24,6 +25,7 @@ public void WhenResultIsExpectedToBeSuccessWithValueItShouldBeSuccessWithValue() var result = Result.Success(value); result.Should().SucceedWith(value); + result.Should().SucceedWith(value).Which.Should().Be(value); } [Fact] @@ -59,6 +61,8 @@ public void WhenResultIsExpectedToBeFailureItShouldBeFailure() action.Should().NotThrow(); actionWith.Should().NotThrow(); + result.Should().Fail().Which.Should().Be(error); + result.Should().FailWith(error).Which.Should().Be(error); } [Fact] diff --git a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/UnitResultAssertionTests.cs b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/UnitResultAssertionTests.cs index 8fa4808..f085efc 100644 --- a/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/UnitResultAssertionTests.cs +++ b/tests/CSharpFunctionalExtensions.FluentAssertions.Tests/UnitResultAssertionTests.cs @@ -37,6 +37,8 @@ public void WhenResultIsExpectedToBeFailureItShouldBeFailure() action.Should().NotThrow(); actionWithError.Should().NotThrow(); + result.Should().Fail().Which.Should().Be(error); + result.Should().FailWith(error).Which.Should().HaveLength(5); } [Fact] From 2eb172d370fdb05e7654b6c93f3d63935f20e8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADcio=20Godoy?= Date: Sat, 6 Apr 2024 15:26:33 -0300 Subject: [PATCH 2/2] Undo Directory.Build changes --- Directory.Build.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 6dd42ce..237e547 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -18,8 +18,8 @@ - - + +