Skip to content

Commit 57bb1ca

Browse files
authored
bugfix: fix string analyzers array complex type ignoring (#243)
* bugfix: fix string analyzers array complex type ignoring * fix code generated by copilot...
1 parent cc32dab commit 57bb1ca

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,12 @@ public class CollectionTests
187187
[AssertionDataTestMethod]
188188
[AssertionDiagnostic("actual.Count().Should().Be(k{0});")]
189189
[AssertionDiagnostic("actual.Count().Should().Be(6{0});")]
190+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(k{0});")]
191+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(6{0});")]
190192
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(k{0}).And.ToString();")]
191193
[AssertionDiagnostic("actual.AsEnumerable().Count().Should().Be(6{0}).And.ToString();")]
194+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(k{0}).And.ToString();")]
195+
[AssertionDiagnostic("actual.ToArray().Length.Should().Be(6{0}).And.ToString();")]
192196
[Implemented]
193197
public void CollectionShouldHaveCount_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock<CollectionShouldHaveCountAnalyzer>(assertion);
194198

@@ -205,6 +209,12 @@ public class CollectionTests
205209
[AssertionCodeFix(
206210
oldAssertion: "actual.Count().Should().Be(6{0});",
207211
newAssertion: "actual.Should().HaveCount(6{0});")]
212+
[AssertionCodeFix(
213+
oldAssertion: "actual.ToArray().Length.Should().Be(6{0});",
214+
newAssertion: "actual.ToArray().Should().HaveCount(6{0});")]
215+
[AssertionCodeFix(
216+
oldAssertion: "actual.ToArray().Length.Should().Be(k{0}).And.ToString();",
217+
newAssertion: "actual.ToArray().Should().HaveCount(k{0}).And.ToString();")]
208218
[AssertionCodeFix(
209219
oldAssertion: "actual.AsEnumerable().Count().Should().Be(k{0}).And.ToString();",
210220
newAssertion: "actual.AsEnumerable().Should().HaveCount(k{0}).And.ToString();")]
@@ -217,6 +227,9 @@ public class CollectionTests
217227
[AssertionCodeFix(
218228
oldAssertion: "actual.AsEnumerable().Count().Should().Be(6{0}).And.ToString();",
219229
newAssertion: "actual.AsEnumerable().Should().HaveCount(6{0}).And.ToString();")]
230+
[AssertionCodeFix(
231+
oldAssertion: "actual.ToArray().Length.Should().Be(6{0}).And.ToString();",
232+
newAssertion: "actual.ToArray().Should().HaveCount(6{0}).And.ToString();")]
220233
[Implemented]
221234
public void CollectionShouldHaveCount_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFixCodeBlock<CollectionShouldHaveCountCodeFix, CollectionShouldHaveCountAnalyzer>(oldAssertion, newAssertion);
222235

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
2424
yield return new CountShouldBe0SyntaxVisitor();
2525
yield return new CountShouldBe1SyntaxVisitor();
2626
yield return new CountShouldBeSyntaxVisitor();
27+
yield return new LengthShouldBeSyntaxVisitor();
2728
}
2829
}
2930

@@ -47,6 +48,13 @@ public class CountShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
4748
{
4849
}
4950
}
51+
52+
public class LengthShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
53+
{
54+
public LengthShouldBeSyntaxVisitor() : base(new MemberValidator("Length"), MemberValidator.Should, new MemberValidator("Be"))
55+
{
56+
}
57+
}
5058
}
5159

5260
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldHaveCountCodeFix)), Shared]
@@ -68,6 +76,10 @@ protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression
6876
{
6977
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
7078
}
79+
else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.LengthShouldBeSyntaxVisitor))
80+
{
81+
return GetNewExpression(expression, NodeReplacement.Remove("Length"), NodeReplacement.Rename("Be", "HaveCount"));
82+
}
7183
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
7284
}
7385
}

src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ namespace FluentAssertions.Analyzers;
55
public abstract class StringAnalyzer : FluentAssertionsAnalyzer
66
{
77
protected override bool ShouldAnalyzeVariableNamedType(INamedTypeSymbol type, SemanticModel semanticModel) => type.SpecialType == SpecialType.System_String;
8+
9+
protected override bool ShouldAnalyzeVariableType(ITypeSymbol type, SemanticModel semanticModel)
10+
{
11+
return false;
12+
}
813
}

0 commit comments

Comments
 (0)