@@ -15,37 +15,61 @@ public class CollectionShouldContainSingleAnalyzer : FluentAssertionsAnalyzer
15
15
public const string DiagnosticId = Constants . Tips . Collections . CollectionShouldContainSingle ;
16
16
public const string Category = Constants . Tips . Category ;
17
17
18
- public const string Message = "Use {0} .Should() followed by ContainSingle() instead." ;
18
+ public const string Message = "Use {0} .Should() followed by . ContainSingle() instead." ;
19
19
20
20
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor ( DiagnosticId , Title , Message , Category , DiagnosticSeverity . Info , true ) ;
21
- protected override IEnumerable < ( FluentAssertionsCSharpSyntaxVisitor , BecauseArgumentsSyntaxVisitor ) > Visitors {
21
+ protected override IEnumerable < ( FluentAssertionsCSharpSyntaxVisitor , BecauseArgumentsSyntaxVisitor ) > Visitors
22
+ {
22
23
get
23
24
{
24
- yield return ( new ShouldHaveCount1SyntaxVisitor ( ) , new BecauseArgumentsSyntaxVisitor ( "HaveCount" , 1 ) ) ;
25
+ yield return ( new WhereShouldHaveCount1SyntaxVisitor ( ) , new BecauseArgumentsSyntaxVisitor ( "HaveCount" , 1 ) ) ;
26
+ yield return ( new ShouldHaveCount1SyntaxVisitor ( ) , new BecauseArgumentsSyntaxVisitor ( "HaveCount" , 1 ) ) ;
25
27
}
26
28
}
27
-
28
- private class ShouldHaveCount1SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
29
+
30
+ private class WhereShouldHaveCount1SyntaxVisitor : FluentAssertionsWithLambdaArgumentCSharpSyntaxVisitor
29
31
{
30
- private bool _haveCountMethodHas0Argument ;
32
+ private bool _haveCountMethodHas1Argument ;
31
33
32
- public override bool IsValid => base . IsValid && _haveCountMethodHas0Argument ;
33
-
34
- public ShouldHaveCount1SyntaxVisitor ( ) : base ( "Should" , "HaveCount" )
34
+ protected override string MethodContainingLambda => "Where" ;
35
+ public override bool IsValid => base . IsValid && _haveCountMethodHas1Argument ;
36
+ public WhereShouldHaveCount1SyntaxVisitor ( ) : base ( "Where" , "Should" , "HaveCount" )
35
37
{
36
38
}
37
39
38
40
public override void VisitArgumentList ( ArgumentListSyntax node )
39
41
{
40
42
if ( ! node . Arguments . Any ( ) ) return ;
41
- if ( CurrentMethod != "HaveCount" ) return ;
43
+ if ( CurrentMethod != "HaveCount" )
44
+ {
45
+ base . VisitArgumentList ( node ) ;
46
+ return ;
47
+ }
42
48
43
- _haveCountMethodHas0Argument =
49
+ _haveCountMethodHas1Argument =
44
50
node . Arguments [ 0 ] . Expression is LiteralExpressionSyntax literal
45
51
&& literal . Token . Value is int argument
46
52
&& argument == 1 ;
47
53
}
48
54
}
55
+ private class ShouldHaveCount1SyntaxVisitor : FluentAssertionsWithArgumentCSharpSyntaxVisitor
56
+ {
57
+ protected override string MethodContainingArgument => "HaveCount" ;
58
+ public ShouldHaveCount1SyntaxVisitor ( ) : base ( "Should" , "HaveCount" )
59
+ {
60
+ }
61
+
62
+ protected override ExpressionSyntax ModifyArgument ( ExpressionSyntax expression )
63
+ {
64
+ if ( expression is LiteralExpressionSyntax literal
65
+ && literal . Token . Value is int argument
66
+ && argument == 1 )
67
+ {
68
+ return expression ;
69
+ }
70
+ return null ;
71
+ }
72
+ }
49
73
}
50
74
51
75
[ ExportCodeFixProvider ( LanguageNames . CSharp , Name = nameof ( CollectionShouldContainSingleCodeFix ) ) , Shared ]
@@ -54,6 +78,6 @@ public class CollectionShouldContainSingleCodeFix : FluentAssertionsCodeFixProvi
54
78
public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( CollectionShouldContainSingleAnalyzer . DiagnosticId ) ;
55
79
56
80
protected override StatementSyntax GetNewStatement ( FluentAssertionsDiagnosticProperties properties )
57
- => SyntaxFactory . ParseStatement ( $ "{ properties . VariableName } .Should().ContainSingle({ properties . BecauseArgumentsString } );") ;
81
+ => SyntaxFactory . ParseStatement ( $ "{ properties . VariableName } .Should().ContainSingle({ properties . CombineWithBecauseArgumentsString ( properties . LambdaString ) } );") ;
58
82
}
59
83
}
0 commit comments