1+ using System . Collections . Generic ;
2+ using System . Collections . Immutable ;
3+ using System . Composition ;
4+ using FluentAssertions . Analyzers . Utilities ;
5+ using Microsoft . CodeAnalysis ;
6+ using Microsoft . CodeAnalysis . CodeFixes ;
7+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
8+ using Microsoft . CodeAnalysis . Diagnostics ;
9+
10+ namespace FluentAssertions . Analyzers . Xunit ;
11+
12+ [ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
13+ public class AssertDoesNotMatchAnalyzer : XunitAnalyzer
14+ {
15+ public const string DiagnosticId = Constants . Tips . Xunit . AssertDoesNotMatch ;
16+ public const string Category = Constants . Tips . Category ;
17+
18+ public const string Message = "Use .Should().NotMatchRegex()" ;
19+
20+ protected override DiagnosticDescriptor Rule => new ( DiagnosticId , Title , Message , Category , DiagnosticSeverity . Info , true ) ;
21+
22+ protected override IEnumerable < FluentAssertionsCSharpSyntaxVisitor > Visitors => new FluentAssertionsCSharpSyntaxVisitor [ ]
23+ {
24+ new AssertDoesNotMatchStringSyntaxVisitor ( )
25+ } ;
26+
27+ //public static void DoesNotMatch(string expectedRegexPattern, string? actualString)
28+ //public static void DoesNotMatch(Regex expectedRegex, string? actualString)
29+ public class AssertDoesNotMatchStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
30+ {
31+ public AssertDoesNotMatchStringSyntaxVisitor ( ) : base (
32+ MemberValidator . ArgumentsMatch ( "DoesNotMatch" ,
33+ ArgumentValidator . IsAnyType ( TypeSelector . GetStringType , TypeSelector . GetRegexType ) ,
34+ ArgumentValidator . IsType ( TypeSelector . GetStringType ) )
35+ )
36+ {
37+ }
38+ }
39+ }
40+
41+ [ ExportCodeFixProvider ( LanguageNames . CSharp , Name = nameof ( AssertDoesNotMatchCodeFix ) ) , Shared ]
42+ public class AssertDoesNotMatchCodeFix : XunitCodeFixProvider
43+ {
44+ public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( AssertDoesNotMatchAnalyzer . DiagnosticId ) ;
45+
46+ protected override ExpressionSyntax GetNewExpression (
47+ ExpressionSyntax expression ,
48+ FluentAssertionsDiagnosticProperties properties )
49+ {
50+ switch ( properties . VisitorName )
51+ {
52+ case nameof ( AssertDoesNotMatchAnalyzer . AssertDoesNotMatchStringSyntaxVisitor ) :
53+ return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould ( expression , "DoesNotMatch" , "NotMatchRegex" ) ;
54+ default :
55+ throw new System . InvalidOperationException ( $ "Invalid visitor name - { properties . VisitorName } ") ;
56+ }
57+ }
58+ }
0 commit comments