Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for StringAsserts Contains (#221)
- Loading branch information
Showing
3 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
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
57 changes: 57 additions & 0 deletions
57
src/FluentAssertions.Analyzers/Tips/Xunit/AssertContains.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,57 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using TypeSelector = FluentAssertions.Analyzers.Utilities.SemanticModelTypeExtensions; | ||
|
||
namespace FluentAssertions.Analyzers.Xunit; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class AssertContainsAnalyzer : XunitAnalyzer | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Xunit.AssertContains; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().Contain()."; | ||
|
||
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[] | ||
{ | ||
new AssertContainsStringSyntaxVisitor() | ||
}; | ||
|
||
//public static void Contains(string expectedSubstring, string? actualString) | ||
public class AssertContainsStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertContainsStringSyntaxVisitor() : base( | ||
MemberValidator.ArgumentsMatch("Contains", | ||
ArgumentValidator.IsType(TypeSelector.GetStringType), | ||
ArgumentValidator.IsType(TypeSelector.GetStringType)) | ||
) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertContainsCodeFix)), Shared] | ||
public class AssertContainsCodeFix : XunitCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertContainsAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression( | ||
ExpressionSyntax expression, | ||
FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
switch (properties.VisitorName) | ||
{ | ||
case nameof(AssertContainsAnalyzer.AssertContainsStringSyntaxVisitor): | ||
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Contains", "Contain"); | ||
default: | ||
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); | ||
} | ||
} | ||
} |