-
-
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 xunit StringAsserts DoesNotMatches (#225)
- Loading branch information
Showing
3 changed files
with
88 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
58 changes: 58 additions & 0 deletions
58
src/FluentAssertions.Analyzers/Tips/Xunit/AssertDoesNotMatch.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,58 @@ | ||
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 AssertDoesNotMatchAnalyzer : XunitAnalyzer | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Xunit.AssertDoesNotMatch; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().NotMatchRegex()"; | ||
|
||
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[] | ||
{ | ||
new AssertDoesNotMatchStringSyntaxVisitor() | ||
}; | ||
|
||
//public static void DoesNotMatch(string expectedRegexPattern, string? actualString) | ||
//public static void DoesNotMatch(Regex expectedRegex, string? actualString) | ||
public class AssertDoesNotMatchStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertDoesNotMatchStringSyntaxVisitor() : base( | ||
MemberValidator.ArgumentsMatch("DoesNotMatch", | ||
ArgumentValidator.IsAnyType(TypeSelector.GetStringType, TypeSelector.GetRegexType), | ||
ArgumentValidator.IsType(TypeSelector.GetStringType)) | ||
) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertDoesNotMatchCodeFix)), Shared] | ||
public class AssertDoesNotMatchCodeFix : XunitCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertDoesNotMatchAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression( | ||
ExpressionSyntax expression, | ||
FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
switch (properties.VisitorName) | ||
{ | ||
case nameof(AssertDoesNotMatchAnalyzer.AssertDoesNotMatchStringSyntaxVisitor): | ||
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "DoesNotMatch", "NotMatchRegex"); | ||
default: | ||
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); | ||
} | ||
} | ||
} |