From 86e61157981320ce3bdf3ec7cdf09aafb38a6291 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Thu, 24 Oct 2024 19:48:07 +0300 Subject: [PATCH] bugfix: improve CollectionShouldHaveCountLessThan_CountShouldBeLessThan detection (#398) --- .../Tips/CollectionTests.cs | 6 ++++++ .../Tips/FluentAssertionsAnalyzer.cs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index 1c6dffd..24be0d1 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -516,6 +516,12 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser [Implemented] public void CollectionShouldHaveCountLessThan_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCountLessThan_CountShouldBeLessThan); + [DataTestMethod] + [AssertionDiagnostic("(actual.Count() + 1).Should().BeLessThan(k{0});")] + [AssertionDiagnostic("(actual.Count() + 1).Should().BeLessThan(6{0});")] + [Implemented] + public void CollectionShouldHaveCountLessThan_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion)); + [DataTestMethod] [AssertionCodeFix( oldAssertion: "actual.Count().Should().BeLessThan(k{0});", diff --git a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs index 41a39f8..8de4cf2 100644 --- a/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs @@ -504,7 +504,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs return; case "BeLessThan" when assertion.IsContainedInType(metadata.NumericAssertionsOfT2): { - if (invocation.TryGetFirstDescendent(out var invocationBeforeShould)) + if (invocation.TryGetSingleArgumentAs(out var invocationBeforeShould)) { switch (invocationBeforeShould.TargetMethod.Name) {