Skip to content

Commit 9d9d685

Browse files
authored
Merge pull request #17 from fluentassertions/feature/GH-12
added support for #12
2 parents 159f69b + 9d166e0 commit 9d9d685

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,24 @@ public class CollectionTests
190190
[AssertionCodeFix(
191191
oldAssertion: "actual.Count().Should().Be(k{0});",
192192
newAssertion: "actual.Should().HaveCount(k{0});")]
193+
[AssertionCodeFix(
194+
oldAssertion: "actual.Count().Should().Be(0{0});",
195+
newAssertion: "actual.Should().BeEmpty({0});")]
196+
[AssertionCodeFix(
197+
oldAssertion: "actual.Count().Should().Be(1{0});",
198+
newAssertion: "actual.Should().ContainSingle({0});")]
193199
[AssertionCodeFix(
194200
oldAssertion: "actual.Count().Should().Be(6{0});",
195201
newAssertion: "actual.Should().HaveCount(6{0});")]
196202
[AssertionCodeFix(
197203
oldAssertion: "actual.AsEnumerable().Count().Should().Be(k{0}).And.ToString();",
198204
newAssertion: "actual.AsEnumerable().Should().HaveCount(k{0}).And.ToString();")]
205+
[AssertionCodeFix(
206+
oldAssertion: "actual.AsEnumerable().Count().Should().Be(0{0}).And.ToString();",
207+
newAssertion: "actual.AsEnumerable().Should().BeEmpty({0}).And.ToString();")]
208+
[AssertionCodeFix(
209+
oldAssertion: "actual.AsEnumerable().Count().Should().Be(1{0}).And.ToString();",
210+
newAssertion: "actual.AsEnumerable().Should().ContainSingle({0}).And.ToString();")]
199211
[AssertionCodeFix(
200212
oldAssertion: "actual.AsEnumerable().Count().Should().Be(6{0}).And.ToString();",
201213
newAssertion: "actual.AsEnumerable().Should().HaveCount(6{0}).And.ToString();")]

src/FluentAssertions.Analyzers/Tips/Collections/CollectionShouldHaveCount.cs

+30-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,27 @@ protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
2121
{
2222
get
2323
{
24+
yield return new CountShouldBe0SyntaxVisitor();
25+
yield return new CountShouldBe1SyntaxVisitor();
2426
yield return new CountShouldBeSyntaxVisitor();
2527
}
2628
}
2729

28-
private class CountShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
30+
public class CountShouldBe0SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
31+
{
32+
public CountShouldBe0SyntaxVisitor() : base(MemberValidator.MathodNotContainingLambda("Count"), MemberValidator.Should, MemberValidator.ArgumentIsLiteral("Be", 0))
33+
{
34+
}
35+
}
36+
37+
public class CountShouldBe1SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
38+
{
39+
public CountShouldBe1SyntaxVisitor() : base(MemberValidator.MathodNotContainingLambda("Count"), MemberValidator.Should, MemberValidator.ArgumentIsLiteral("Be", 1))
40+
{
41+
}
42+
}
43+
44+
public class CountShouldBeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
2945
{
3046
public CountShouldBeSyntaxVisitor() : base(MemberValidator.MathodNotContainingLambda("Count"), MemberValidator.Should, new MemberValidator("Be"))
3147
{
@@ -40,7 +56,19 @@ public class CollectionShouldHaveCountCodeFix : FluentAssertionsCodeFixProvider
4056

4157
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
4258
{
43-
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
59+
if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBe0SyntaxVisitor))
60+
{
61+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "BeEmpty"));
62+
}
63+
else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBe1SyntaxVisitor))
64+
{
65+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.RenameAndRemoveFirstArgument("Be", "ContainSingle"));
66+
}
67+
else if (properties.VisitorName == nameof(CollectionShouldHaveCountAnalyzer.CountShouldBeSyntaxVisitor))
68+
{
69+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
70+
}
71+
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
4472
}
4573
}
4674
}

0 commit comments

Comments
 (0)