From 8b409799809160915877f14ebf4e7996a111f4c2 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Sat, 28 Oct 2023 22:27:44 +0300 Subject: [PATCH 1/2] bugfix: fix string analyzers array complex type ignoring --- .../Tips/CollectionTests.cs | 13 +++++++++++++ .../Tips/Collections/CollectionShouldHaveCount.cs | 12 ++++++++++++ .../Tips/Strings/StringAnalyzer.cs | 5 +++++ 3 files changed, 30 insertions(+) diff --git a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs index c928f378..44274376 100644 --- a/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs +++ b/src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs @@ -187,8 +187,12 @@ public class CollectionTests [AssertionDataTestMethod] [AssertionDiagnostic("actual.Count().Should().Be(k{0});")] [AssertionDiagnostic("actual.Count().Should().Be(6{0});")] + [AssertionDiagnostic("actual.ToArray().Length.Should().Be(k{0});")] + [AssertionDiagnostic("actual.ToArray().Length.Should().Be(6{0});")] [AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(k{0}).And.ToString();")] [AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(6{0}).And.ToString();")] + [AssertionDiagnostic("actual.ToArray().Length.Should().Be(k{0}).And.ToString();")] + [AssertionDiagnostic("actual.ToArray().Length.Should().Be(6{0}).And.ToString();")] [Implemented] public void CollectionShouldHaveCount_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock(assertion); @@ -205,6 +209,12 @@ public class CollectionTests [AssertionCodeFix( oldAssertion: "actual.Count().Should().Be(6{0});", newAssertion: "actual.Should().HaveCount(6{0});")] + [AssertionCodeFix( + oldAssertion: "actual.ToArray().Length.Should().Be(6{0});", + newAssertion: "actual.ToArray().Should().HaveCount(6{0});")] + [AssertionCodeFix( + oldAssertion: "actual.ToArray().Length.Should().Be(k{0}).And.ToString();", + newAssertion: "actual.ToArray().Should().HaveCount(k{0}).And.ToString();")] [AssertionCodeFix( oldAssertion: "actual.AsEnumerable().Count().Should().Be(k{0}).And.ToString();", newAssertion: "actual.AsEnumerable().Should().HaveCount(k{0}).And.ToString();")] @@ -217,6 +227,9 @@ public class CollectionTests [AssertionCodeFix( oldAssertion: "actual.AsEnumerable().Count().Should().Be(6{0}).And.ToString();", newAssertion: "actual.AsEnumerable().Should().HaveCount(6{0}).And.ToString();")] + [AssertionCodeFix( + oldAssertion: "actual.ToArray().Length.Should().Be(6{0}).And.ToString();", + newAssertion: "actual.ToArray().Should().HaveCount(6{0}).And.ToString();")] [Implemented] public void CollectionShouldHaveCount_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFixCodeBlock(oldAssertion, newAssertion); diff --git a/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs b/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs index e5da9694..a1a46830 100644 --- a/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs +++ b/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs @@ -24,6 +24,7 @@ protected override IEnumerable Visitors yield return new CountShouldBe0SyntaxVisitor(); yield return new CountShouldBe1SyntaxVisitor(); yield return new CountShouldBeSyntaxVisitor(); + yield return new LengthShouldBeSyntaxVisitor(); } } @@ -47,6 +48,13 @@ public class CountShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor { } } + + public class LengthShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor + { + public LengthShouldBeSyntaxVisitor() : base(new MemberValidator("Length"), MemberValidator.Should, new MemberValidator("Be")) + { + } + } } [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldHaveCountCodeFix)), Shared] @@ -68,6 +76,10 @@ protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression { return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount")); } + else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.LengthShouldBeSyntaxVisitor)) + { + return GetNewExpression(expression, NodeReplacement.Remove("Length"), NodeReplacement.Rename("Be", "HaveLength")); + } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); } } diff --git a/src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs b/src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs index 4990e642..9aa92701 100644 --- a/src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs +++ b/src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs @@ -5,4 +5,9 @@ namespace FluentAssertions.Analyzers; public abstract class StringAnalyzer : FluentAssertionsAnalyzer { protected override bool ShouldAnalyzeVariableNamedType(INamedTypeSymbol type, SemanticModel semanticModel) => type.SpecialType == SpecialType.System_String; + + protected override bool ShouldAnalyzeVariableType(ITypeSymbol type, SemanticModel semanticModel) + { + return false; + } } From 4d81c22b5f6720a8b0f466d7597dbff7a7908b10 Mon Sep 17 00:00:00 2001 From: Meir Blachman Date: Sat, 28 Oct 2023 22:32:58 +0300 Subject: [PATCH 2/2] fix code generated by copilot... --- .../Tips/Collections/CollectionShouldHaveCount.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs b/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs index a1a46830..fe1d5b80 100644 --- a/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs +++ b/src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs @@ -78,7 +78,7 @@ protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression } else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.LengthShouldBeSyntaxVisitor)) { - return GetNewExpression(expression, NodeReplacement.Remove("Length"), NodeReplacement.Rename("Be", "HaveLength")); + return GetNewExpression(expression, NodeReplacement.Remove("Length"), NodeReplacement.Rename("Be", "HaveCount")); } throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); }