-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce single CollectionAnalyzer for collection assertions (#251)
* introduce single CollectionAnalyzer for collection assertions * Restore help links for collection assertions * cleanup * consolidate CollectionShouldNotContainProperty into collection analyzer * Update CollectionTests.cs and remove CollectionShouldHaveElementAt0Null.cs * Refactor collection intersection code * revert some changes * update TODOs * cleanup collections
- Loading branch information
Showing
33 changed files
with
413 additions
and
1,160 deletions.
There are no files selected for viewing
112 changes: 56 additions & 56 deletions
112
src/FluentAssertions.Analyzers.Tests/Tips/CollectionTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
239 changes: 229 additions & 10 deletions
239
src/FluentAssertions.Analyzers/Tips/Collections/CollectionAnalyzer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/FluentAssertions.Analyzers/Tips/Collections/CollectionBaseAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using FluentAssertions.Analyzers.Utilities; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace FluentAssertions.Analyzers; | ||
|
||
public abstract class CollectionBaseAnalyzer : FluentAssertionsAnalyzer | ||
{ | ||
protected override bool ShouldAnalyzeVariableNamedType(INamedTypeSymbol type, SemanticModel semanticModel) | ||
{ | ||
return type.SpecialType != SpecialType.System_String | ||
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T); | ||
} | ||
|
||
protected override bool ShouldAnalyzeVariableType(ITypeSymbol type, SemanticModel semanticModel) | ||
{ | ||
return type.SpecialType != SpecialType.System_String | ||
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 3 additions & 42 deletions
45
src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldBeInAscendingOrder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
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 CollectionShouldBeInAscendingOrderAnalyzer : CollectionAnalyzer | ||
public static class CollectionShouldBeInAscendingOrder | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldBeInAscendingOrder; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().BeInAscendingOrder() instead."; | ||
|
||
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors | ||
{ | ||
get | ||
{ | ||
yield return new OrderByShouldEqualSyntaxVisitor(); | ||
} | ||
} | ||
|
||
public class OrderByShouldEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public OrderByShouldEqualSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("OrderBy"), MemberValidator.Should, MemberValidator.ArgumentIsVariable("Equal")) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeInAscendingOrderCodeFix)), Shared] | ||
public class CollectionShouldBeInAscendingOrderCodeFix : FluentAssertionsCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInAscendingOrderAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
var remove = NodeReplacement.RemoveAndExtractArguments("OrderBy"); | ||
var newExpression = GetNewExpression(expression, remove); | ||
|
||
newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInAscendingOrder")); | ||
|
||
return GetNewExpression(newExpression, NodeReplacement.PrependArguments("BeInAscendingOrder", remove.Arguments)); | ||
} | ||
} | ||
} |
45 changes: 3 additions & 42 deletions
45
src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldBeInDescendingOrder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,11 @@ | ||
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 CollectionShouldBeInDescendingOrderAnalyzer : CollectionAnalyzer | ||
public static class CollectionShouldBeInDescendingOrder | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldBeInDescendingOrder; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().BeInDescendingOrder() instead."; | ||
|
||
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors | ||
{ | ||
get | ||
{ | ||
yield return new OrderByDescendingShouldEqualSyntaxVisitor(); | ||
} | ||
} | ||
|
||
public class OrderByDescendingShouldEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public OrderByDescendingShouldEqualSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("OrderByDescending"), MemberValidator.Should, MemberValidator.ArgumentIsVariable("Equal")) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeInDescendingOrderCodeFix)), Shared] | ||
public class CollectionShouldBeInDescendingOrderCodeFix : FluentAssertionsCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInDescendingOrderAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
var remove = NodeReplacement.RemoveAndExtractArguments("OrderByDescending"); | ||
var newExpression = GetNewExpression(expression, remove); | ||
|
||
newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInDescendingOrder")); | ||
|
||
return GetNewExpression(newExpression, NodeReplacement.PrependArguments("BeInDescendingOrder", remove.Arguments)); | ||
} | ||
} | ||
} |
44 changes: 3 additions & 41 deletions
44
src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldContainItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,11 @@ | ||
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 CollectionShouldContainItemAnalyzer : CollectionAnalyzer | ||
public static class CollectionShouldContainItem | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldContainItem; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().Contain() instead."; | ||
|
||
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors | ||
{ | ||
get | ||
{ | ||
yield return new ContainsShouldBeTrueSyntaxVisitor(); | ||
} | ||
} | ||
|
||
public class ContainsShouldBeTrueSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public ContainsShouldBeTrueSyntaxVisitor() : base(new MemberValidator("Contains"), MemberValidator.Should, new MemberValidator("BeTrue")) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldContainItemCodeFix)), Shared] | ||
public class CollectionShouldContainItemCodeFix : FluentAssertionsCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldContainItemAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
var remove = NodeReplacement.RemoveAndExtractArguments("Contains"); | ||
var newExpression = GetNewExpression(expression, remove); | ||
|
||
return GetNewExpression(newExpression, NodeReplacement.RenameAndPrependArguments("BeTrue", "Contain", remove.Arguments)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.