Skip to content

feat: improve TryGetFirstDescendent logic - p1 #386

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 6 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
41 changes: 38 additions & 3 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,30 @@ public class CollectionTests
public void CollectionShouldHaveCount_CountShouldBe1_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldContainSingle_CountShouldBe1);

[DataTestMethod]
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(1{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveCount_LengthShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe);
[AssertionDiagnostic("(array.Count() + 1).Should().Be(0{0}).And.ToString();")]
[AssertionDiagnostic("(array.Count() + 1).Should().Be(1{0}).And.ToString();")]
[AssertionDiagnostic("(array.Count() + 1).Should().Be(expectedSize{0}).And.ToString();")]
[AssertionDiagnostic("(list.Count + 1).Should().Be(0{0}).And.ToString();")]
[AssertionDiagnostic("(list.Count + 1).Should().Be(1{0}).And.ToString();")]
[AssertionDiagnostic("(list.Count + 1).Should().Be(expectedSize{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveCount_CountShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(new StringBuilder()
.AppendLine("using System;")
.AppendLine("using System.Collections.Generic;")
.AppendLine("using System.Linq;")
.AppendLine("using FluentAssertions;")
.AppendLine("using FluentAssertions.Extensions;")
.AppendLine("namespace TestNamespace")
.AppendLine("{")
.AppendLine(" public class TestClass")
.AppendLine(" {")
.AppendLine(" public void TestMethod(string[] array, List<string> list, int expectedSize)")
.AppendLine(" {")
.AppendLine(assertion)
.AppendLine(" }")
.AppendLine(" }")
.AppendLine("}")
.ToString());

[DataTestMethod]
[AssertionDiagnostic(@"var array = new string[0, 0]; array.Length.Should().Be(0{0});")]
Expand Down Expand Up @@ -610,6 +631,12 @@ public void CollectionShouldHaveCount_LengthShouldBe_TestNoAnalyzer(string asser
[Implemented]
public void CollectionShouldNotHaveSameCount_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldNotHaveSameCount_CountShouldNotBeOtherCollectionCount);

[DataTestMethod]
[AssertionDiagnostic("(actual.Count() + 1).Should().NotBe(unexpected.Count(){0});")]
[AssertionDiagnostic("actual.Count().ToString().Length.Should().NotBe(unexpected.Count(){0});")]
[Implemented]
public void CollectionShouldNotHaveSameCount_TestNotAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Count().Should().NotBe(unexpected.Count(){0});",
Expand Down Expand Up @@ -742,6 +769,14 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
[Implemented]
public void CollectionShouldHaveElementAt_ElementAtIndexShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveElementAt_ElementAtIndexShouldBe);

[DataTestMethod]
[AssertionDiagnostic("actual.ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
[AssertionDiagnostic("actual.ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0});")]
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(k).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
[AssertionDiagnostic("actual.AsEnumerable().ElementAt(6).BooleanProperty.Should().Be(expectedItem.BooleanProperty{0}).And.ToString();")]
[Implemented]
public void CollectionShouldHaveElementAt_ElementAtIndexShouldBe_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionDiagnostic("actual[k].Should().Be(expectedItem{0});")]
[AssertionDiagnostic("actual[6].Should().Be(expectedItem{0});")]
Expand Down
12 changes: 6 additions & 6 deletions src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public const string DiagnosticId = "FAA0001";
public const string Message = "Clean up FluentAssertion usage";

protected static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Constants.Tips.Category, DiagnosticSeverity.Info, true);

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / Performance regression check

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 19 in src/FluentAssertions.Analyzers/Tips/FluentAssertionsAnalyzer.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FAA0001' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

Expand Down Expand Up @@ -301,7 +301,7 @@
return;
case "OnlyHaveUniqueItems" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3):
{
if (!invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould)) return;
if (!invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould)) return;
switch (invocationBeforeShould.TargetMethod.Name)
{
case nameof(Enumerable.Select) when IsEnumerableMethodWithPredicate(invocationBeforeShould, metadata):
Expand All @@ -312,7 +312,7 @@
return;
case "Be" 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 @@ -334,8 +334,8 @@

}
}
var argument = invocation.Arguments[0].Value.UnwrapConversion();
if (argument is IPropertyReferenceOperation propertyBeforeShould)

if (invocation.TryGetSingleArgumentAs<IPropertyReferenceOperation>(out var propertyBeforeShould))
{
switch (propertyBeforeShould.Property.Name)
{
Expand Down Expand Up @@ -370,7 +370,7 @@
}
case "Be" when assertion.IsContainedInType(metadata.ObjectAssertionsOfT2):
{
if (invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould))
if (invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould))
{
switch (invocationBeforeShould.TargetMethod.Name)
{
Expand Down Expand Up @@ -402,7 +402,7 @@
return;
case "NotBe" when assertion.IsContainedInType(metadata.NumericAssertionsOfT2):
{
if (invocation.TryGetFirstDescendent<IInvocationOperation>(out var invocationBeforeShould))
if (invocation.TryGetSingleArgumentAs<IInvocationOperation>(out var invocationBeforeShould))
{
switch (invocationBeforeShould.TargetMethod.Name)
{
Expand Down
12 changes: 12 additions & 0 deletions src/FluentAssertions.Analyzers/Utilities/OperartionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public static bool TryGetChainedInvocationAfterAndConstraint(this IInvocationOpe
return false;
}

public static bool TryGetSingleArgumentAs<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