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