-
-
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.
add support for StringAsserts DoesNotContain (#223)
- Loading branch information
Showing
3 changed files
with
73 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/AssertDoesNotContain.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 FluentAssertions.Analyzers.Utilities; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace FluentAssertions.Analyzers.Xunit; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class AssertDoesNotContainAnalyzer : XunitAnalyzer | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Xunit.AssertDoesNotContain; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().NotContain()."; | ||
|
||
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[] | ||
{ | ||
new AssertDoesNotContainStringSyntaxVisitor() | ||
}; | ||
|
||
//public static void DoesNotContain(string expectedSubstring, string? actualString) | ||
public class AssertDoesNotContainStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertDoesNotContainStringSyntaxVisitor() : base( | ||
MemberValidator.ArgumentsMatch("DoesNotContain", | ||
ArgumentValidator.IsType(TypeSelector.GetStringType), | ||
ArgumentValidator.IsType(TypeSelector.GetStringType)) | ||
) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertDoesNotContainCodeFix)), Shared] | ||
public class AssertDoesNotContainCodeFix : XunitCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertDoesNotContainAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression( | ||
ExpressionSyntax expression, | ||
FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
switch (properties.VisitorName) | ||
{ | ||
case nameof(AssertDoesNotContainAnalyzer.AssertDoesNotContainStringSyntaxVisitor): | ||
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "DoesNotContain", "NotContain"); | ||
default: | ||
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); | ||
} | ||
} | ||
} |