Skip to content

Commit 90021b5

Browse files
committed
{EnumerableAnalyzer} - 2 more
1 parent 946062d commit 90021b5

File tree

6 files changed

+73
-68
lines changed

6 files changed

+73
-68
lines changed

src/FluentAssertions.BestPractices.Tests/Tips/CollectionTests.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,35 @@ public class CollectionTests
103103

104104
[AssertionDataTestMethod]
105105
[AssertionDiagnostic("actual.Count().Should().Be(k{0});")]
106-
[NotImplemented]
106+
[AssertionDiagnostic("actual.Count().Should().Be(6{0});")]
107+
[Implemented]
107108
public void CollectionShouldHaveCount_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<CollectionShouldHaveCountAnalyzer>(assertion);
108109

109110
[AssertionDataTestMethod]
110111
[AssertionCodeFix(
111112
oldAssertion: "actual.Count().Should().Be(k{0});",
112113
newAssertion: "actual.Should().HaveCount(k{0});")]
113-
[NotImplemented]
114+
[AssertionCodeFix(
115+
oldAssertion: "actual.Count().Should().Be(6{0});",
116+
newAssertion: "actual.Should().HaveCount(6{0});")]
117+
[Implemented]
114118
public void CollectionShouldHaveCount_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<CollectionShouldHaveCountCodeFix, CollectionShouldHaveCountAnalyzer>(oldAssertion, newAssertion);
115119

116120
[AssertionDataTestMethod]
117121
[AssertionDiagnostic("actual.Count().Should().BeGreaterThan(k{0});")]
118-
[NotImplemented]
122+
[AssertionDiagnostic("actual.Count().Should().BeGreaterThan(6{0});")]
123+
[Implemented]
119124
public void CollectionShouldHaveCountGreaterThan_TestAnalyzer(string assertion) => VerifyCSharpDiagnostic<CollectionShouldHaveCountGreaterThanAnalyzer>(assertion);
120125

121126
[AssertionDataTestMethod]
122127
[AssertionCodeFix(
123128
oldAssertion: "actual.Count().Should().BeGreaterThan(k{0});",
124129
newAssertion: "actual.Should().HaveCountGreaterThan(k{0});")]
125-
[NotImplemented]
130+
[AssertionCodeFix(
131+
oldAssertion: "actual.Count().Should().BeGreaterThan(6{0});",
132+
newAssertion: "actual.Should().HaveCountGreaterThan(6{0});")]
133+
[Implemented]
134+
[Ignore("Waiting for official FluentAssertions 5.0")]
126135
public void CollectionShouldHaveCountGreaterThan_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<CollectionShouldHaveCountGreaterThanCodeFix, CollectionShouldHaveCountGreaterThanAnalyzer>(oldAssertion, newAssertion);
127136

128137
[AssertionDataTestMethod]
@@ -135,6 +144,7 @@ public class CollectionTests
135144
oldAssertion: "actual.Count().Should().BeGreaterOrEqualTo(k{0});",
136145
newAssertion: "actual.Should().HaveCountGreaterOrEqualTo(k{0});")]
137146
[NotImplemented]
147+
[Ignore("Waiting for official FluentAssertions 5.0")]
138148
public void CollectionShouldHaveCountGreaterOrEqualTo_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<CollectionShouldHaveCountGreaterOrEqualToCodeFix, CollectionShouldHaveCountGreaterOrEqualToAnalyzer>(oldAssertion, newAssertion);
139149

140150
[AssertionDataTestMethod]
@@ -147,6 +157,7 @@ public class CollectionTests
147157
oldAssertion: "actual.Count().Should().BeLessThan(k{0});",
148158
newAssertion: "actual.Should().HaveCountLessThan(k{0});")]
149159
[NotImplemented]
160+
[Ignore("Waiting for official FluentAssertions 5.0")]
150161
public void CollectionShouldHaveCountLessThan_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<CollectionShouldHaveCountLessThanCodeFix, CollectionShouldHaveCountLessThanAnalyzer>(oldAssertion, newAssertion);
151162

152163
[AssertionDataTestMethod]
@@ -207,7 +218,7 @@ public class CollectionTests
207218
newAssertion: "actual.Should().NotHaveSameCount(unexpected{0});")]
208219
[NotImplemented]
209220
public void CollectionShouldNotHaveSameCount_TestCodeFix(string oldAssertion, string newAssertion) => VerifyCSharpFix<CollectionShouldNotHaveSameCountCodeFix, CollectionShouldNotHaveSameCountAnalyzer>(oldAssertion, newAssertion);
210-
221+
211222
[AssertionDataTestMethod]
212223
[AssertionDiagnostic("actual.Where(x => x.BooleanProperty).Should().BeEmpty({0});")]
213224
[NotImplemented]

src/FluentAssertions.BestPractices/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public static class DiagnosticProperties
1010
public const string ExpectedItemString = nameof(ExpectedItemString);
1111
public const string UnexpectedItemString = nameof(UnexpectedItemString);
1212
public const string BecauseArgumentsString = nameof(BecauseArgumentsString);
13+
public const string CountArgument = nameof(CountArgument);
1314
}
1415

1516
public static class Tips

src/FluentAssertions.BestPractices/Tips/Collections/CollectionShouldBeEmpty.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public ShouldHaveCount0SyntaxVisitor() : base("Should", "HaveCount")
4848
public override void VisitArgumentList(ArgumentListSyntax node)
4949
{
5050
if (!node.Arguments.Any()) return;
51+
if (CurrentMethod != "HaveCount") return;
5152

52-
_haveCountMethodHas0Argument = !_haveCountMethodHas0Argument
53-
&& CurrentMethod == "HaveCount"
54-
&& node.Arguments[0].Expression is LiteralExpressionSyntax literal
53+
_haveCountMethodHas0Argument =
54+
node.Arguments[0].Expression is LiteralExpressionSyntax literal
5555
&& literal.Token.Value is int argument
5656
&& argument == 0;
5757
}
@@ -62,8 +62,8 @@ public override void VisitArgumentList(ArgumentListSyntax node)
6262
public class CollectionShouldBeEmptyCodeFix : FluentAssertionsCodeFixProvider
6363
{
6464
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeEmptyAnalyzer.DiagnosticId);
65-
65+
6666
protected override StatementSyntax GetNewStatement(FluentAssertionsDiagnosticProperties properties)
6767
=> SyntaxFactory.ParseStatement($"{properties.VariableName}.Should().BeEmpty({properties.BecauseArgumentsString});");
68-
}
68+
}
6969
}
Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CodeFixes;
3+
using Microsoft.CodeAnalysis.CSharp;
34
using Microsoft.CodeAnalysis.CSharp.Syntax;
45
using Microsoft.CodeAnalysis.Diagnostics;
56
using System.Collections.Generic;
@@ -14,32 +15,37 @@ public class CollectionShouldHaveCountAnalyzer : FluentAssertionsAnalyzer
1415
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldHaveCount;
1516
public const string Category = Constants.Tips.Category;
1617

17-
public const string Message = "Use {0} .Should() followed by ### instead.";
18+
public const string Message = "Use {0} .Should() followed by .HaveCount() instead.";
1819

1920
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
21+
protected override IEnumerable<(FluentAssertionsCSharpSyntaxVisitor, BecauseArgumentsSyntaxVisitor)> Visitors
22+
{
23+
get
24+
{
25+
yield return (new CountShouldBeSyntaxVisitor(), new BecauseArgumentsSyntaxVisitor("Be", 1));
26+
}
27+
}
2028

21-
protected override Diagnostic AnalyzeExpressionStatement(ExpressionStatementSyntax statement)
29+
private class CountShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
2230
{
23-
return null;
24-
var visitor = new CollectionShouldHaveCountSyntaxVisitor();
25-
statement.Accept(visitor);
31+
public string CountArgument { get; private set; }
2632

27-
if (visitor.IsValid)
33+
public override bool IsValid => base.IsValid && CountArgument != null;
34+
35+
public CountShouldBeSyntaxVisitor() : base("Count", "Should", "Be")
2836
{
29-
var properties = new Dictionary<string, string>
30-
{
31-
[Constants.DiagnosticProperties.VariableName] = visitor.VariableName,
32-
[Constants.DiagnosticProperties.Title] = Title
33-
}.ToImmutableDictionary();
34-
throw new System.NotImplementedException();
37+
}
3538

36-
return Diagnostic.Create(
37-
descriptor: Rule,
38-
location: statement.Expression.GetLocation(),
39-
properties: properties,
40-
messageArgs: visitor.VariableName);
39+
public override ImmutableDictionary<string, string> ToDiagnosticProperties()
40+
=> base.ToDiagnosticProperties().Add(Constants.DiagnosticProperties.CountArgument, CountArgument);
41+
42+
public override void VisitArgumentList(ArgumentListSyntax node)
43+
{
44+
if (!node.Arguments.Any()) return;
45+
if (CurrentMethod != "Be") return;
46+
47+
CountArgument = node.Arguments[0].ToFullString();
4148
}
42-
return null;
4349
}
4450
}
4551

@@ -49,16 +55,6 @@ public class CollectionShouldHaveCountCodeFix : FluentAssertionsCodeFixProvider
4955
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountAnalyzer.DiagnosticId);
5056

5157
protected override StatementSyntax GetNewStatement(FluentAssertionsDiagnosticProperties properties)
52-
{
53-
throw new System.NotImplementedException();
54-
}
55-
}
56-
57-
public class CollectionShouldHaveCountSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
58-
{
59-
public CollectionShouldHaveCountSyntaxVisitor() : base("###")
60-
{
61-
throw new System.NotImplementedException();
62-
}
58+
=> SyntaxFactory.ParseStatement($"{properties.VariableName}.Should().HaveCount({properties.CombineWithBecauseArgumentsString(properties.CountArgument)});");
6359
}
6460
}
Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CodeFixes;
3+
using Microsoft.CodeAnalysis.CSharp;
34
using Microsoft.CodeAnalysis.CSharp.Syntax;
45
using Microsoft.CodeAnalysis.Diagnostics;
56
using System.Collections.Generic;
@@ -14,32 +15,37 @@ public class CollectionShouldHaveCountGreaterThanAnalyzer : FluentAssertionsAnal
1415
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldHaveCountGreaterThan;
1516
public const string Category = Constants.Tips.Category;
1617

17-
public const string Message = "Use {0} .Should() followed by ### instead.";
18+
public const string Message = "Use {0} .Should() followed by .BeGreaterThan() instead.";
1819

1920
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
21+
protected override IEnumerable<(FluentAssertionsCSharpSyntaxVisitor, BecauseArgumentsSyntaxVisitor)> Visitors
22+
{
23+
get
24+
{
25+
yield return (new CountShouldBeGreaterThanSyntaxVisitor(), new BecauseArgumentsSyntaxVisitor("BeGreaterThan", 1));
26+
}
27+
}
2028

21-
protected override Diagnostic AnalyzeExpressionStatement(ExpressionStatementSyntax statement)
29+
private class CountShouldBeGreaterThanSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
2230
{
23-
return null;
24-
var visitor = new CollectionShouldHaveCountGreaterThanSyntaxVisitor();
25-
statement.Accept(visitor);
31+
public string CountArgument { get; private set; }
2632

27-
if (visitor.IsValid)
33+
public override bool IsValid => base.IsValid && CountArgument != null;
34+
35+
public CountShouldBeGreaterThanSyntaxVisitor() : base("Count", "Should", "BeGreaterThan")
2836
{
29-
var properties = new Dictionary<string, string>
30-
{
31-
[Constants.DiagnosticProperties.VariableName] = visitor.VariableName,
32-
[Constants.DiagnosticProperties.Title] = Title
33-
}.ToImmutableDictionary();
34-
throw new System.NotImplementedException();
37+
}
3538

36-
return Diagnostic.Create(
37-
descriptor: Rule,
38-
location: statement.Expression.GetLocation(),
39-
properties: properties,
40-
messageArgs: visitor.VariableName);
39+
public override ImmutableDictionary<string, string> ToDiagnosticProperties()
40+
=> base.ToDiagnosticProperties().Add(Constants.DiagnosticProperties.CountArgument, CountArgument);
41+
42+
public override void VisitArgumentList(ArgumentListSyntax node)
43+
{
44+
if (!node.Arguments.Any()) return;
45+
if (CurrentMethod != "BeGreaterThan") return;
46+
47+
CountArgument = node.Arguments[0].ToFullString();
4148
}
42-
return null;
4349
}
4450
}
4551

@@ -49,16 +55,6 @@ public class CollectionShouldHaveCountGreaterThanCodeFix : FluentAssertionsCodeF
4955
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountGreaterThanAnalyzer.DiagnosticId);
5056

5157
protected override StatementSyntax GetNewStatement(FluentAssertionsDiagnosticProperties properties)
52-
{
53-
throw new System.NotImplementedException();
54-
}
55-
}
56-
57-
public class CollectionShouldHaveCountGreaterThanSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
58-
{
59-
public CollectionShouldHaveCountGreaterThanSyntaxVisitor() : base("###")
60-
{
61-
throw new System.NotImplementedException();
62-
}
58+
=> SyntaxFactory.ParseStatement($"{properties.VariableName}.Should().HaveCountGreaterThan({properties.CombineWithBecauseArgumentsString(properties.CountArgument)});");
6359
}
6460
}

src/FluentAssertions.BestPractices/Utilities/FluentAssertionsDiagnosticProperties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public FluentAssertionsDiagnosticProperties(ImmutableDictionary<string, string>
1818
public string PredicateString => Properties[Constants.DiagnosticProperties.PredicateString];
1919
public string ExpectedItemString => Properties[Constants.DiagnosticProperties.ExpectedItemString];
2020
public string UnexpectedItemString => Properties[Constants.DiagnosticProperties.UnexpectedItemString];
21+
public string CountArgument => Properties[Constants.DiagnosticProperties.CountArgument];
2122

2223
public string CombineWithBecauseArgumentsString(string validArgument)
2324
{

0 commit comments

Comments
 (0)