Skip to content

Commit

Permalink
bugfix: fix analyzer for issue #300 and #299 (#303)
Browse files Browse the repository at this point in the history
* bugfix: fix analyzer for issue #300

* bugfix: fix issue 299
  • Loading branch information
Meir017 authored Jan 24, 2024
1 parent edf3129 commit b15a52e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
21 changes: 20 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 @@ -903,6 +917,11 @@ public void CollectionShouldContainSingle_TestAnalyzer_GenericIEnumerableShouldR
[Implemented]
public void CollectionShouldOnlyHaveUniqueItems_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion, DiagnosticMetadata.CollectionShouldOnlyHaveUniqueItems_ShouldHaveSameCountThisCollectionDistinct);

[DataTestMethod]
[AssertionDiagnostic("actual.Should().HaveSameCount(expected.Distinct(){0});")]
[Implemented(Reason = "https://github.com/fluentassertions/fluentassertions.analyzers/issues/299")]
public void CollectionShouldOnlyHaveUniqueItems_TestNoAnalyzer(string assertion) => DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(GenerateCode.GenericIListCodeBlockAssertion(assertion));

[DataTestMethod]
[AssertionCodeFix(
oldAssertion: "actual.Should().HaveSameCount(actual.Distinct(){0});",
Expand Down Expand Up @@ -1032,7 +1051,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 @@ -57,7 +57,8 @@ private static void AnalyzeInvocation(OperationAnalysisContext context, FluentAs
return;
}

var subject = invocation.Arguments[0].Value;
var subjectArgument = invocation.Arguments[0];
var subject = subjectArgument.Value;

switch (assertion.TargetMethod.Name)
{
Expand Down Expand Up @@ -289,10 +290,11 @@ 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) } assertionArgumentInvocation
&& assertionArgumentInvocation.Arguments[0].IsSameArgumentReference(subjectArgument):
context.ReportDiagnostic(CreateDiagnostic(assertion, DiagnosticMetadata.CollectionShouldOnlyHaveUniqueItems_ShouldHaveSameCountThisCollectionDistinct));
return;
case "OnlyHaveUniqueItems" when assertion.IsContainedInType(metadata.GenericCollectionAssertionsOfT3):
Expand Down

0 comments on commit b15a52e

Please sign in to comment.