From f66d37ef0a0599ca654f22d37750c9cdb86adc08 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Tue, 22 Oct 2024 12:19:10 +0300 Subject: [PATCH] bugfix: improve CollectionShouldHaveCountLessOrEqualTo_CountShouldBeLessOrEqualTo & NumericShouldBeApproximately_MathAbsShouldBeLessOrEqualTo detection --- .../Tips/CollectionTests.cs | 6 ++++++ src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs | 5 +++++ .../Tips/FluentAssertionsAnalyzer.cs | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index c62d8eb6..1c6dffd9 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -556,6 +556,12 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser [Implemented] public void CollectionShouldHaveCountLessOrEqualTo_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCountLessOrEqualTo_CountShouldBeLessOrEqualTo); + [DataTestMethod] + [AssertionDiagnostic("(actual.Count() + 1).Should().BeLessOrEqualTo(k{0});")] + [AssertionDiagnostic("(actual.Count() + 1).Should().BeLessOrEqualTo(6{0});")] + [Implemented] + public void CollectionShouldHaveCountLessOrEqualTo_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion)); + [DataTestMethod] [AssertionCodeFix( oldAssertion: "actual.Count().Should().BeLessOrEqualTo(k{0});", diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs index f03d4445..985d91fd 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/NumericTests.cs @@ -93,6 +93,11 @@ void verifyNoDiagnostic(string type) [Implemented] public void NumericShouldBeApproximately_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic(assertion, DiagnosticMetadata.NumericShouldBeApproximately_MathAbsShouldBeLessOrEqualTo); + [DataTestMethod] + [AssertionDiagnostic("(Math.Abs(expected - actual) + 1).Should().BeLessOrEqualTo(delta{0});")] + [Implemented] + public void NumericShouldBeApproximately_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.NumericAssertion(assertion, "double")); + [DataTestMethod] [AssertionCodeFix( oldAssertion: "Math.Abs(expected - actual).Should().BeLessOrEqualTo(delta{0});", diff --git a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs index f11c3ade..41a39f8c 100644 --- a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs @@ -475,7 +475,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs return; case "BeLessOrEqualTo" when assertion.IsContainedInType(metadata.NumericAssertionsOfT2): { - if (invocation.TryGetFirstDescendent(out var invocationBeforeShould)) + if (invocation.TryGetSingleArgumentAs(out var invocationBeforeShould)) { switch (invocationBeforeShould.TargetMethod.Name) {