Skip to content

Commit

Permalink
consolidate CollectionShouldNotContainProperty into collection analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 committed Dec 10, 2023
1 parent d699008 commit e06a4e6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class CollectionTests
[AssertionDiagnostic("actual.AsEnumerable().Where(x => x.BooleanProperty).Should().BeEmpty({0}).And.ToString();")]
[AssertionDiagnostic("actual.AsEnumerable().Should().OnlyContain(x => !x.BooleanProperty{0}).And.ToString();", ignore: true)]
[Implemented]
public void CollectionShouldNotContainProperty_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock<CollectionShouldNotContainPropertyAnalyzer>(assertion);
public void CollectionShouldNotContainProperty_TestAnalyzer(string assertion) => VerifyCSharpDiagnosticCodeBlock<CollectionAnalyzer>(assertion);

[AssertionDataTestMethod]
[AssertionCodeFix(
Expand All @@ -135,7 +135,7 @@ public class CollectionTests
newAssertion: "actual.AsEnumerable().Should().NotContain(x => x.BooleanProperty{0}).And.ToString();",
ignore: true)]
[Implemented]
public void CollectionShouldNotContainProperty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFixCodeBlock<CollectionShouldNotContainPropertyCodeFix, CollectionShouldNotContainPropertyAnalyzer>(oldAssertion, newAssertion);
public void CollectionShouldNotContainProperty_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFixCodeBlock<CollectionCodeFix, CollectionAnalyzer>(oldAssertion, newAssertion);

[AssertionDataTestMethod]
[AssertionDiagnostic("actual.All(x => x.BooleanProperty).Should().BeTrue({0});")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors

yield return new CollectionShouldNotContainNulls.SelectShouldNotContainNullsSyntaxVisitor();

// TODO: Add support for CollectionShouldNotContainPropertyAnalyzer
yield return new CollectionShouldNotContainProperty.AnyLambdaShouldBeFalseSyntaxVisitor();
yield return new CollectionShouldNotContainProperty.WhereShouldBeEmptySyntaxVisitor();
// TODO: enable this:
// yield return new CollectionShouldNotContainProperty.ShouldOnlyContainNotSyntaxVisitor();

yield return new CollectionShouldNotHaveCount.CountShouldNotBeSyntaxVisitor();
yield return new CollectionShouldNotHaveSameCount.CountShouldNotBeOtherCollectionCountSyntaxVisitor();
Expand Down Expand Up @@ -187,6 +190,28 @@ protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression

return GetNewExpression(newExpression, NodeReplacement.PrependArguments("NotContainNulls", remove.Arguments));
}

case nameof(CollectionShouldNotContainProperty.AnyLambdaShouldBeFalseSyntaxVisitor):
{
var remove = NodeReplacement.RemoveAndExtractArguments("Any");
var newExpression = GetNewExpression(expression, remove);

return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeFalse", "NotContain", remove.Arguments));
}
case nameof(CollectionShouldNotContainProperty.WhereShouldBeEmptySyntaxVisitor):
{
var remove = NodeReplacement.RemoveAndExtractArguments("Where");
var newExpression = GetNewExpression(expression, remove);

return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeEmpty", "NotContain", remove.Arguments));
}
/*
case nameof(CollectionShouldNotContainProperty.ShouldOnlyContainNotSyntaxVisitor):
{
return GetNewExpression(expression, NodeReplacement.RenameAndNegateLambda("OnlyContain", "NotContain"));
}
*/

case nameof(CollectionShouldNotHaveCount.CountShouldNotBeSyntaxVisitor):
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("NotBe", "NotHaveCount")); ;
case nameof(CollectionShouldNotHaveSameCount.CountShouldNotBeOtherCollectionCountSyntaxVisitor):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
namespace FluentAssertions.Analyzers;

namespace FluentAssertions.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionShouldNotContainPropertyAnalyzer : CollectionAnalyzer
public static class CollectionShouldNotContainProperty
{
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldNotContainProperty;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().NotContain() instead.";

protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
{
get
{
yield return new AnyShouldBeFalseSyntaxVisitor();
yield return new WhereShouldBeEmptySyntaxVisitor();
// TODO: yield return new ShouldOnlyContainNotSyntaxVisitor();
}
}

public class AnyShouldBeFalseSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
public class AnyLambdaShouldBeFalseSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AnyShouldBeFalseSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("Any"), MemberValidator.Should, new MemberValidator("BeFalse"))
public AnyLambdaShouldBeFalseSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("Any"), MemberValidator.Should, new MemberValidator("BeFalse"))
{
}
}
Expand All @@ -47,35 +22,4 @@ public ShouldOnlyContainSyntaxVisitor() : base(MemberValidator.Should, MemberVal
{
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldNotContainPropertyCodeFix)), Shared]
public class CollectionShouldNotContainPropertyCodeFix : FluentAssertionsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldNotContainPropertyAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.AnyShouldBeFalseSyntaxVisitor))
{
var remove = NodeReplacement.RemoveAndExtractArguments("Any");
var newExpression = GetNewExpression(expression, remove);

return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeFalse", "NotContain", remove.Arguments));
}
else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.WhereShouldBeEmptySyntaxVisitor))
{
var remove = NodeReplacement.RemoveAndExtractArguments("Where");
var newExpression = GetNewExpression(expression, remove);

return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeEmpty", "NotContain", remove.Arguments));
}
/*
else if (properties.VisitorName == nameof(CollectionShouldNotContainPropertyAnalyzer.ShouldOnlyContainNotSyntaxVisitor))
{
return GetNewExpression(expression, NodeReplacement.RenameAndNegateLambda("OnlyContain", "NotContain"));
}
*/
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
}
}
}

0 comments on commit e06a4e6

Please sign in to comment.