Skip to content

bugfix: improve CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount detection #393

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

Merged
merged 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser

[DataTestMethod]
[AssertionDiagnostic("(actual.Count() + 1).Should().NotBe(unexpected.Count(){0});")]
[AssertionDiagnostic("actual.Count().Should().NotBe((unexpected.Count() + 1){0});")]
[AssertionDiagnostic("actual.Count().ToString().Length.Should().NotBe(unexpected.Count(){0});")]
[Implemented]
public void CollectionShouldNotHaveSameCount_TestNotAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
{
// TODO: add support for Enumerable.LongCount
case nameof(Enumerable.Count) when IsEnumerableMethodWithoutArguments(invocationBeforeShould, metadata):
if (assertion.Arguments[0].HasFirstDescendentInvocation(nameof(Enumerable.Count)))
if (assertion.TryGetFirstArgumentAs<IInvocationOperation>(out var assertionInvocation) && IsEnumerableMethodWithoutArguments(assertionInvocation, metadata))
{
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount));
}
Expand Down
17 changes: 12 additions & 5 deletions src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public static TOperation GetFirstAncestor<TOperation>(this IOperation parent) wh
return default;
}

public static bool HasFirstDescendentInvocation(this IOperation parent, string invocationMethod)
{
return parent.TryGetFirstDescendent<IInvocationOperation>(out var invocation) && invocation.TargetMethod.Name == invocationMethod;
}

public static bool IsContainedInType(this IPropertyReferenceOperation property, SpecialType type)
=> property.Property.ContainingType.ConstructedFromType(type);
public static bool IsContainedInType(this IPropertyReferenceOperation property, INamedTypeSymbol type)
Expand Down Expand Up @@ -204,6 +199,18 @@ public static bool TryGetSingleArgumentAs<TOperation>(this IInvocationOperation
return false;
}

public static bool TryGetFirstArgumentAs<TOperation>(this IInvocationOperation invocation, out TOperation operation)
{
if (invocation.Arguments.Length is >= 1 && invocation.Arguments[0].Value.UnwrapConversion() is TOperation op)
{
operation = op;
return true;
}

operation = default;
return false;
}

public static IOperation UnwrapConversion(this IOperation operation)
{
return operation switch
Expand Down
Loading