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 AssertEmptyAnalyzer : XunitAnalyzer
14+ {
15+ public const string DiagnosticId = Constants . Tips . Xunit . AssertEmpty ;
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 AssertEmptyStringSyntaxVisitor ( )
25+ } ;
26+
27+ //public static void Empty(string value)
28+ public class AssertEmptyStringSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
29+ {
30+ public AssertEmptyStringSyntaxVisitor ( ) : base (
31+ MemberValidator . ArgumentsMatch ( "Empty" ,
32+ ArgumentValidator . IsType ( TypeSelector . GetStringType ) )
33+ )
34+ {
35+ }
36+ }
37+ }
38+
39+ [ ExportCodeFixProvider ( LanguageNames . CSharp , Name = nameof ( AssertEmptyCodeFix ) ) , Shared ]
40+ public class AssertEmptyCodeFix : XunitCodeFixProvider
41+ {
42+ public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( AssertEmptyAnalyzer . DiagnosticId ) ;
43+
44+ protected override ExpressionSyntax GetNewExpression (
45+ ExpressionSyntax expression ,
46+ FluentAssertionsDiagnosticProperties properties )
47+ {
48+ switch ( properties . VisitorName )
49+ {
50+ case nameof ( AssertEmptyAnalyzer . AssertEmptyStringSyntaxVisitor ) :
51+ return RenameMethodAndReplaceWithSubjectShould ( expression , "Empty" , "BeEmpty" ) ;
52+ default :
53+ throw new System . InvalidOperationException ( $ "Invalid visitor name - { properties . VisitorName } ") ;
54+ }
55+ }
56+ }
0 commit comments