-
-
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 Matches (#224)
* add support for StringAsserts Matches * add using System.Text.RegularExpressions for xunit tests
- Loading branch information
Showing
6 changed files
with
94 additions
and
1 deletion.
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
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/AssertMatches.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 AssertMatchesAnalyzer : XunitAnalyzer | ||
{ | ||
public const string DiagnosticId = Constants.Tips.Xunit.AssertMatches; | ||
public const string Category = Constants.Tips.Category; | ||
|
||
public const string Message = "Use .Should().MatchRegex()"; | ||
|
||
protected override DiagnosticDescriptor Rule => new(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true); | ||
|
||
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors => new FluentAssertionsCSharpSyntaxVisitor[] | ||
{ | ||
new AssertMatchesStringSyntaxVisitor() | ||
}; | ||
|
||
//public static void Matches(string expectedRegexPattern, string? actualString) | ||
//public static void Matches(Regex expectedRegex, string? actualString) | ||
public class AssertMatchesStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor | ||
{ | ||
public AssertMatchesStringSyntaxVisitor() : base( | ||
MemberValidator.ArgumentsMatch("Matches", | ||
ArgumentValidator.IsAnyType(TypeSelector.GetStringType, TypeSelector.GetRegexType), | ||
ArgumentValidator.IsType(TypeSelector.GetStringType)) | ||
) | ||
{ | ||
} | ||
} | ||
} | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AssertMatchesCodeFix)), Shared] | ||
public class AssertMatchesCodeFix : XunitCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AssertMatchesAnalyzer.DiagnosticId); | ||
|
||
protected override ExpressionSyntax GetNewExpression( | ||
ExpressionSyntax expression, | ||
FluentAssertionsDiagnosticProperties properties) | ||
{ | ||
switch (properties.VisitorName) | ||
{ | ||
case nameof(AssertMatchesAnalyzer.AssertMatchesStringSyntaxVisitor): | ||
return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould(expression, "Matches", "MatchRegex"); | ||
default: | ||
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}"); | ||
} | ||
} | ||
} |
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