Skip to content

Commit

Permalink
bugfix: fix analyzer for issue #300
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 committed Jan 24, 2024
1 parent 2c718db commit 2522752
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ public class CollectionTests
[Implemented]
public void CollectionShouldHaveCount_LengthShouldBe_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveCount_LengthShouldBe);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + 1{0});")]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + unexpected.Count(){0});")]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count + unexpected.Count{0});")]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/300")]
public void CollectionShouldHaveCount_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Count().Should().Be(k{0});",
Expand Down Expand Up @@ -533,6 +540,13 @@ public class CollectionTests
[Implemented]
public void CollectionShouldHaveSameCount_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldHaveSameCount_ShouldHaveCountOtherCollectionCount);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + 1{0});")]
[AssertionDiagnostic("actual.Should().HaveCount(expected.Count() + unexpected.Count(){0});")]
[AssertionDiagnostic("actual.Should().HaveCount(1 + expected.Count(){0});")]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/300")]
public void CollectionShouldHaveSameCount_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Should().HaveCount(expected.Count(){0});",
Expand Down Expand Up @@ -1032,7 +1046,7 @@ private void VerifyCSharpFixExpressionBody(string oldSourceAssertion, string new
{
var oldSource = GenerateCode.GenericIListExpressionBodyAssertion(oldSourceAssertion);
var newSource = GenerateCode.GenericIListExpressionBodyAssertion(newSourceAssertion);

VerifyFix(oldSource, newSource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,10 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
case "HaveCount" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3) && assertion.Arguments[0].IsLiteralValue(0):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldBeEmpty_ShouldHaveCount0));
return;
case "HaveCount" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3) && assertion.Arguments[0].HasFirstDescendentInvocation(nameof(Enumerable.Count)):
case "HaveCount" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3) && assertion.Arguments[0].Value is IInvocationOperation { TargetMethod.Name: nameof(Enumerable.Count) }:
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldHaveSameCount_ShouldHaveCountOtherCollectionCount));
return;
case "HaveSameCount" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3) && assertion.Arguments[0].HasFirstDescendentInvocation(nameof(Enumerable.Distinct)):
case "HaveSameCount" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3) && assertion.Arguments[0].Value is IInvocationOperation { TargetMethod.Name: nameof(Enumerable.Distinct) }:
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldOnlyHaveUniqueItems_ShouldHaveSameCountThisCollectionDistinct));
return;
case "OnlyHaveUniqueItems" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3):
Expand Down

0 comments on commit 2522752

Please sign in to comment.