Skip to content

Commit dd587f4

Browse files
authored
Merge pull request #15 from fluentassertions/feature/expression-body-analyzers
added support for expressionBody
2 parents aca496f + 8cade13 commit dd587f4

File tree

42 files changed

+300
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+300
-221
lines changed

src/FluentAssertions.Analyzers.Tests/GenerateCode.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
using System.Text;
1+
using System;
2+
using System.Text;
23

34
namespace FluentAssertions.Analyzers.Tests
45
{
56
public static class GenerateCode
67
{
78
public static string ActualVariableName => "actual";
89

9-
public static string EnumerableAssertion(string assertion) => new StringBuilder()
10+
public static string EnumerableCodeBlockAssertion(string assertion) => EnumerableAssertion(
11+
" {" + Environment.NewLine +
12+
" " + assertion + Environment.NewLine +
13+
" }");
14+
public static string EnumerableExpressionBodyAssertion(string assertion) => EnumerableAssertion(
15+
" => " + assertion);
16+
17+
private static string EnumerableAssertion(string bodyExpression) => new StringBuilder()
1018
.AppendLine("using System.Collections.Generic;")
1119
.AppendLine("using System.Linq;")
1220
.AppendLine("using System;")
@@ -16,9 +24,7 @@ public static class GenerateCode
1624
.AppendLine(" public class TestClass")
1725
.AppendLine(" {")
1826
.AppendLine($" public void TestMethod(IList<TestComplexClass> {ActualVariableName}, IList<TestComplexClass> expected, IList<TestComplexClass> unexpected, TestComplexClass expectedItem, TestComplexClass unexpectedItem, int k)")
19-
.AppendLine(" {")
20-
.AppendLine($" {assertion}")
21-
.AppendLine(" }")
27+
.AppendLine(bodyExpression)
2228
.AppendLine(" }")
2329
.AppendLine(" public class TestComplexClass")
2430
.AppendLine(" {")

src/FluentAssertions.Analyzers.Tests/TestAttributes.cs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ namespace FluentAssertions.Analyzers.Tests
88
[AttributeUsage(AttributeTargets.Method)]
99
public class NotImplementedAttribute : TestCategoryBaseAttribute
1010
{
11+
public string Reason { get; set; }
12+
1113
public override IList<string> TestCategories => new[] { "NotImplemented" };
1214
}
1315

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

+107-61
Large diffs are not rendered by default.

src/FluentAssertions.Analyzers.Tests/Tips/DictionaryTests.generated.cs

Whitespace-only changes.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class SanityTests
1010
public void AssertionCallMultipleMethodWithTheSameNameAndArguments()
1111
{
1212
const string assertion = "actual.Should().Contain(d => d.Message.Contains(\"a\")).And.Contain(d => d.Message.Contains(\"c\"));";
13-
var source = GenerateCode.EnumerableAssertion(assertion);
13+
var source = GenerateCode.EnumerableCodeBlockAssertion(assertion);
1414

1515
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
1616
}
@@ -20,7 +20,7 @@ public void AssertionCallMultipleMethodWithTheSameNameAndArguments()
2020
public void PropertyOfIndexerShouldBe_ShouldNotThrowException()
2121
{
2222
const string assertion = "actual[0].Message.Should().Be(\"test\");";
23-
var source = GenerateCode.EnumerableAssertion(assertion);
23+
var source = GenerateCode.EnumerableCodeBlockAssertion(assertion);
2424

2525
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
2626
}
@@ -30,7 +30,7 @@ public void PropertyOfIndexerShouldBe_ShouldNotThrowException()
3030
public void PropertyOfElementAtShouldBe_ShouldNotTriggerDiagnostic()
3131
{
3232
const string assertion = "actual.ElementAt(0).Message.Should().Be(\"test\");";
33-
var source = GenerateCode.EnumerableAssertion(assertion);
33+
var source = GenerateCode.EnumerableCodeBlockAssertion(assertion);
3434

3535
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
3636
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ public class CollectionShouldBeEmptyCodeFix : FluentAssertionsCodeFixProvider
6363
{
6464
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeEmptyAnalyzer.DiagnosticId);
6565

66-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
66+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
6767
{
6868
switch (properties.VisitorName)
6969
{
7070
case nameof(CollectionShouldBeEmptyAnalyzer.AnyShouldBeFalseSyntaxVisitor):
71-
return GetNewStatement(statement, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeFalse", "BeEmpty"));
71+
return GetNewExpression(expression, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeFalse", "BeEmpty"));
7272
case nameof(CollectionShouldBeEmptyAnalyzer.ShouldHaveCount0SyntaxVisitor):
73-
return GetNewStatement(statement, new HaveCountNodeReplacement());
73+
return GetNewExpression(expression, new HaveCountNodeReplacement());
7474
default:
7575
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
7676
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public class CollectionShouldBeInAscendingOrderCodeFix : FluentAssertionsCodeFix
5555
{
5656
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInAscendingOrderAnalyzer.DiagnosticId);
5757

58-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
58+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
5959
{
6060
var remove = NodeReplacement.RemoveAndExtractArguments("OrderBy");
61-
var newStatement = GetNewStatement(statement, remove);
61+
var newStatement = GetNewExpression(expression, remove);
6262

63-
newStatement = GetNewStatement(newStatement, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInAscendingOrder"));
63+
newStatement = GetNewExpression(newStatement, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInAscendingOrder"));
6464

65-
return GetNewStatement(newStatement, NodeReplacement.PrependArguments("BeInAscendingOrder", remove.Arguments));
65+
return GetNewExpression(newStatement, NodeReplacement.PrependArguments("BeInAscendingOrder", remove.Arguments));
6666
}
6767
}
6868
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public class CollectionShouldBeInDescendingOrderCodeFix : FluentAssertionsCodeFi
5555
{
5656
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInDescendingOrderAnalyzer.DiagnosticId);
5757

58-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
58+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
5959
{
6060
var remove = NodeReplacement.RemoveAndExtractArguments("OrderByDescending");
61-
var newStatement = GetNewStatement(statement, remove);
61+
var newStatement = GetNewExpression(expression, remove);
6262

63-
newStatement = GetNewStatement(newStatement, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInDescendingOrder"));
63+
newStatement = GetNewExpression(newStatement, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInDescendingOrder"));
6464

65-
return GetNewStatement(newStatement, NodeReplacement.PrependArguments("BeInDescendingOrder", remove.Arguments));
65+
return GetNewExpression(newStatement, NodeReplacement.PrependArguments("BeInDescendingOrder", remove.Arguments));
6666
}
6767
}
6868
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public class CollectionShouldContainItemCodeFix : FluentAssertionsCodeFixProvide
5353
{
5454
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldContainItemAnalyzer.DiagnosticId);
5555

56-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
56+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
5757
{
5858
var remove = new NodeReplacement.RemoveAndExtractArgumentsNodeReplacement("Contains");
59-
var newStatement = GetNewStatement(statement, remove);
59+
var newStatement = GetNewExpression(expression, remove);
6060

61-
return GetNewStatement(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeTrue", "Contain", remove.Arguments));
61+
return GetNewExpression(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeTrue", "Contain", remove.Arguments));
6262
}
6363
}
6464
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,21 @@ public class CollectionShouldContainPropertyCodeFix : FluentAssertionsCodeFixPro
4747
{
4848
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldContainPropertyAnalyzer.DiagnosticId);
4949

50-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
50+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
5151
{
5252
if (properties.VisitorName == nameof(CollectionShouldContainPropertyAnalyzer.AnyShouldBeTrueSyntaxVisitor))
5353
{
5454
var remove = new NodeReplacement.RemoveAndExtractArgumentsNodeReplacement("Any");
55-
var newStatement = GetNewStatement(statement, remove);
55+
var newStatement = GetNewExpression(expression, remove);
5656

57-
return GetNewStatement(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeTrue", "Contain", remove.Arguments));
57+
return GetNewExpression(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("BeTrue", "Contain", remove.Arguments));
5858
}
5959
else if (properties.VisitorName == nameof(CollectionShouldContainPropertyAnalyzer.WhereShouldNotBeEmptySyntaxVisitor))
6060
{
6161
var remove = new NodeReplacement.RemoveAndExtractArgumentsNodeReplacement("Where");
62-
var newStatement = GetNewStatement(statement, remove);
62+
var newStatement = GetNewExpression(expression, remove);
6363

64-
return GetNewStatement(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("NotBeEmpty", "Contain", remove.Arguments));
64+
return GetNewExpression(newStatement, new NodeReplacement.RenameAndPrependArgumentsNodeReplacement("NotBeEmpty", "Contain", remove.Arguments));
6565
}
6666
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
6767
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,19 @@ public class CollectionShouldContainSingleCodeFix : FluentAssertionsCodeFixProvi
7676
{
7777
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldContainSingleAnalyzer.DiagnosticId);
7878

79-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
79+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
8080
{
81-
var newStatement = GetNewStatement(statement, NodeReplacement.RenameAndRemoveFirstArgument("HaveCount", "ContainSingle"));
81+
var newStatement = GetNewExpression(expression, NodeReplacement.RenameAndRemoveFirstArgument("HaveCount", "ContainSingle"));
8282
if (properties.VisitorName == nameof(CollectionShouldContainSingleAnalyzer.ShouldHaveCount1SyntaxVisitor))
8383
{
8484
return newStatement;
8585
}
8686
else if (properties.VisitorName == nameof(CollectionShouldContainSingleAnalyzer.WhereShouldHaveCount1SyntaxVisitor))
8787
{
8888
var remove = NodeReplacement.RemoveAndExtractArguments("Where");
89-
newStatement = GetNewStatement(newStatement, remove);
89+
newStatement = GetNewExpression(newStatement, remove);
9090

91-
return GetNewStatement(newStatement, NodeReplacement.PrependArguments("ContainSingle", remove.Arguments));
91+
return GetNewExpression(newStatement, NodeReplacement.PrependArguments("ContainSingle", remove.Arguments));
9292
}
9393
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
9494
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public class CollectionShouldEqualOtherCollectionByComparerCodeFix : FluentAsser
7878
{
7979
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldEqualOtherCollectionByComparerAnalyzer.DiagnosticId);
8080

81-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
81+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
8282
{
8383
var removeMethodContainingFirstLambda = NodeReplacement.RemoveAndExtractArguments("Select");
84-
var newStatement = GetNewStatement(statement, removeMethodContainingFirstLambda);
84+
var newStatement = GetNewExpression(expression, removeMethodContainingFirstLambda);
8585

8686
var removeArgument = NodeReplacement.RemoveFirstArgument("Equal");
87-
newStatement = GetNewStatement(newStatement, removeArgument);
87+
newStatement = GetNewExpression(newStatement, removeArgument);
8888

8989
var argumentInvocation = (InvocationExpressionSyntax)removeArgument.Argument.Expression;
9090
var identifier = ((MemberAccessExpressionSyntax)argumentInvocation.Expression).Expression;
@@ -97,7 +97,7 @@ protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax sta
9797
.Add(removeArgument.Argument.WithExpression(CombineLambdas(firstLambda, secondLambda).NormalizeWhitespace()
9898
));
9999

100-
return GetNewStatement(newStatement, NodeReplacement.PrependArguments("Equal", newArguments));
100+
return GetNewExpression(newStatement, NodeReplacement.PrependArguments("Equal", newArguments));
101101
}
102102

103103
private ParenthesizedLambdaExpressionSyntax CombineLambdas(SimpleLambdaExpressionSyntax left, SimpleLambdaExpressionSyntax right) => SyntaxFactory.ParenthesizedLambdaExpression(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ public class CollectionShouldHaveCountCodeFix : FluentAssertionsCodeFixProvider
5353
{
5454
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountAnalyzer.DiagnosticId);
5555

56-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
56+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
5757
{
58-
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
58+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("Be", "HaveCount"));
5959
}
6060
}
6161
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class CollectionShouldHaveCountGreaterOrEqualToCodeFix : FluentAssertions
3939
{
4040
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountGreaterOrEqualToAnalyzer.DiagnosticId);
4141

42-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
42+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterOrEqualTo", "HaveCountGreaterOrEqualTo"));
44+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterOrEqualTo", "HaveCountGreaterOrEqualTo"));
4545
}
4646
}
4747
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public class CollectionShouldHaveCountGreaterThanCodeFix : FluentAssertionsCodeF
4040
{
4141
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountGreaterThanAnalyzer.DiagnosticId);
4242

43-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
43+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
4444
{
45-
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterThan", "HaveCountGreaterThan"));
45+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeGreaterThan", "HaveCountGreaterThan"));
4646
}
4747
}
4848
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class CollectionShouldHaveCountLessOrEqualToCodeFix : FluentAssertionsCod
3939
{
4040
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountLessOrEqualToAnalyzer.DiagnosticId);
4141

42-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
42+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessOrEqualTo", "HaveCountLessOrEqualTo"));
44+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessOrEqualTo", "HaveCountLessOrEqualTo"));
4545
}
4646
}
4747
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public class CollectionShouldHaveCountLessThanCodeFix : FluentAssertionsCodeFixP
3939
{
4040
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldHaveCountLessThanAnalyzer.DiagnosticId);
4141

42-
protected override StatementSyntax GetNewStatement(ExpressionStatementSyntax statement, FluentAssertionsDiagnosticProperties properties)
42+
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
4343
{
44-
return GetNewStatement(statement, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessThan", "HaveCountLessThan"));
44+
return GetNewExpression(expression, NodeReplacement.Remove("Count"), NodeReplacement.Rename("BeLessThan", "HaveCountLessThan"));
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)