Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: improve CollectionShouldHaveCountGreaterOrEqualTo_CountShouldBeGreaterOrEqualTo detection #392

Merged
merged 1 commit into from
Oct 21, 2024
Merged
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
26 changes: 26 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,18 +451,44 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser
[DataTestMethod]
[AssertionDiagnostic("actual.Count().Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("actual.Count().Should().BeGreaterOrEqualTo(6{0});")]
[AssertionDiagnostic("actual.Count.Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("actual.Count.Should().BeGreaterOrEqualTo(6{0});")]
[AssertionDiagnostic("actual.ToArray().Length.Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("actual.ToArray().Length.Should().BeGreaterOrEqualTo(6{0});")]
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().BeGreaterOrEqualTo(k{0}).And.ToString();")]
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().BeGreaterOrEqualTo(6{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveCountGreaterOrEqualTo_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCountGreaterOrEqualTo_CountShouldBeGreaterOrEqualTo);

[DataTestMethod]
[AssertionDiagnostic("(actual.Count() + 1).Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("(actual.Count() + 1).Should().BeGreaterOrEqualTo(6{0});")]
[AssertionDiagnostic("(actual.Count + 1).Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("(actual.Count + 1).Should().BeGreaterOrEqualTo(6{0});")]
[AssertionDiagnostic("(actual.ToArray().Length + 1).Should().BeGreaterOrEqualTo(k{0});")]
[AssertionDiagnostic("(actual.ToArray().Length + 1).Should().BeGreaterOrEqualTo(6{0});")]
[Implemented]
public void CollectionShouldHaveCountGreaterOrEqualTo_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Count().Should().BeGreaterOrEqualTo(k{0});",
newAssertion: "actual.Should().HaveCountGreaterOrEqualTo(k{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Count().Should().BeGreaterOrEqualTo(6{0});",
newAssertion: "actual.Should().HaveCountGreaterOrEqualTo(6{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Count.Should().BeGreaterOrEqualTo(k{0});",
newAssertion: "actual.Should().HaveCountGreaterOrEqualTo(k{0});")]
[AssertionCodeFix(
oldAssertion: "actual.Count.Should().BeGreaterOrEqualTo(6{0});",
newAssertion: "actual.Should().HaveCountGreaterOrEqualTo(6{0});")]
[AssertionCodeFix(
oldAssertion: "actual.ToArray().Length.Should().BeGreaterOrEqualTo(k{0});",
newAssertion: "actual.ToArray().Should().HaveCountGreaterOrEqualTo(k{0});")]
[AssertionCodeFix(
oldAssertion: "actual.ToArray().Length.Should().BeGreaterOrEqualTo(6{0});",
newAssertion: "actual.ToArray().Should().HaveCountGreaterOrEqualTo(6{0});")]
[AssertionCodeFix(
oldAssertion: "actual.AsEnumerable().Count().Should().BeGreaterOrEqualTo(k{0}).And.ToString();",
newAssertion: "actual.AsEnumerable().Should().HaveCountGreaterOrEqualTo(k{0}).And.ToString();")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public FluentAssertionsMetadata(Compilation compilation)
IReadonlyDictionaryOfT2 = compilation.GetTypeByMetadataName(typeof(IReadOnlyDictionary<,>).FullName);
IListOfT = compilation.GetTypeByMetadataName(typeof(IList<>).FullName);
IReadonlyListOfT = compilation.GetTypeByMetadataName(typeof(IReadOnlyList<>).FullName);
ICollectionOfT = compilation.GetTypeByMetadataName(typeof(ICollection<>).FullName);
IReadonlyCollectionOfT = compilation.GetTypeByMetadataName(typeof(IReadOnlyCollection<>).FullName);
Enumerable = compilation.GetTypeByMetadataName(typeof(Enumerable).FullName);
IEnumerable = compilation.GetTypeByMetadataName(typeof(IEnumerable).FullName);
Math = compilation.GetTypeByMetadataName(typeof(Math).FullName);
Expand All @@ -91,6 +93,8 @@ public FluentAssertionsMetadata(Compilation compilation)
public INamedTypeSymbol IReadonlyDictionaryOfT2 { get; }
public INamedTypeSymbol IListOfT { get; }
public INamedTypeSymbol IReadonlyListOfT { get; }
public INamedTypeSymbol ICollectionOfT { get; }
public INamedTypeSymbol IReadonlyCollectionOfT { get; }
public INamedTypeSymbol BooleanAssertionsOfT1 { get; }
public INamedTypeSymbol NumericAssertionsOfT2 { get; }
public INamedTypeSymbol Enumerable { get; }
Expand Down
13 changes: 12 additions & 1 deletion src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
return;
case "BeGreaterOrEqualTo" when assertion.IsContainedInType(metadata.NumericAssertionsOfT2):
{
if (invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould))
if (invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould))
{
switch (invocationBeforeShould.TargetMethod.Name)
{
Expand All @@ -434,6 +434,17 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
}
}

if (invocation.TryGetSingleArgumentAs<IPropertyReferenceOperation>(out var propertyBeforeShould))
{
switch (propertyBeforeShould.Property.Name)
{
case nameof(Array.Length) when propertyBeforeShould.IsContainedInType(SpecialType.System_Array):
case nameof(List<object>.Count) when propertyBeforeShould.ImplementsOrIsInterface(SpecialType.System_Collections_Generic_ICollection_T):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveCountGreaterOrEqualTo_CountShouldBeGreaterOrEqualTo));
return;
}
}

if (assertion.TryGetChainedInvocationAfterAndConstraint("BeLessOrEqualTo", out var chainedInvocation))
{
if (!assertion.HasEmptyBecauseAndReasonArgs(startingIndex: 1) && !chainedInvocation.HasEmptyBecauseAndReasonArgs(startingIndex: 1)) return;
Expand Down
Loading