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