Skip to content

Commit

Permalink
bugfix: fix string analyzers array complex type ignoring (#243)
Browse files Browse the repository at this point in the history
* bugfix: fix string analyzers array complex type ignoring

* fix code generated by copilot...
  • Loading branch information
Meir017 authored Oct 28, 2023
1 parent cc32dab commit 57bb1ca
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CollectionShouldHaveCountAnalyzer>(assertion);

Expand All @@ -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();")]
Expand All @@ -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<CollectionShouldHaveCountCodeFix, CollectionShouldHaveCountAnalyzer>(oldAssertion, newAssertion);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
yield return new CountShouldBe0SyntaxVisitor();
yield return new CountShouldBe1SyntaxVisitor();
yield return new CountShouldBeSyntaxVisitor();
yield return new LengthShouldBeSyntaxVisitor();
}
}

Expand All @@ -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]
Expand All @@ -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", "HaveCount"));
}
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
}
}
5 changes: 5 additions & 0 deletions src/FluentAssertions.Analyzers/Tips/Strings/StringAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 57bb1ca

Please sign in to comment.