Skip to content

Commit f7212c1

Browse files
use RegisterSyntaxNodeAction instead of RegisterCodeBlockAction (#167)
* use RegisterSyntaxNodeAction instead of RegisterCodeBlockAction * test: nested asserts inside code blocks * test: actual is method invocation --------- Co-authored-by: Meir Blachman <meblachm@microsoft.com>
1 parent 3eb6906 commit f7212c1

File tree

2 files changed

+63
-24
lines changed

2 files changed

+63
-24
lines changed

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

+50-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,53 @@ public void ShouldEquals_ShouldBe_ObjectType_TestCodeFix()
2222
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
2323
}
2424

25+
[TestMethod]
26+
[Implemented]
27+
public void ShouldEquals_NestedInsideIfBlock_TestAnalyzer()
28+
=> VerifyCSharpDiagnosticExpressionBody("if(true) { actual.Should().Equals(expected); }", 10, 24);
29+
30+
[TestMethod]
31+
[Implemented]
32+
public void ShouldEquals_NestedInsideIfBlock_ShouldBe_ObjectType_TestCodeFix()
33+
{
34+
var oldSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Equals(expected); }");
35+
var newSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Be(expected); }");
36+
37+
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
38+
}
39+
40+
[TestMethod]
41+
[Implemented]
42+
public void ShouldEquals_NestedInsideWhileBlock_TestAnalyzer()
43+
=> VerifyCSharpDiagnosticExpressionBody("while(true) { actual.Should().Equals(expected); }", 10, 27);
44+
45+
[TestMethod]
46+
[Implemented]
47+
public void ShouldEquals_NestedInsideWhileBlock_ShouldBe_ObjectType_TestCodeFix()
48+
{
49+
var oldSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Equals(expected); }");
50+
var newSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Be(expected); }");
51+
52+
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
53+
}
54+
55+
[TestMethod]
56+
[Implemented]
57+
public void ShouldEquals_ActualIsMethodInvoaction_TestAnalyzer()
58+
=> VerifyCSharpDiagnosticExpressionBody("object ResultSupplier() { return null; } \n"
59+
+ "ResultSupplier().Should().Equals(expected);", 11, 0);
60+
61+
[TestMethod]
62+
[Implemented]
63+
public void ShouldEquals_ActualIsMethodInvoaction_ShouldBe_ObjectType_TestCodeFix()
64+
{
65+
const string methodInvocation = "object ResultSupplier() { return null; } \n";
66+
var oldSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Equals(expected);");
67+
var newSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Be(expected);");
68+
69+
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
70+
}
71+
2572
[TestMethod]
2673
[Implemented]
2774
public void ShouldEquals_ShouldBe_NumberType_TestCodeFix()
@@ -52,7 +99,8 @@ public void ShouldEquals_ShouldEqual_EnumerableType_TestCodeFix()
5299
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
53100
}
54101

55-
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion)
102+
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion) => VerifyCSharpDiagnosticExpressionBody(sourceAssertion, 10, 13);
103+
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, int line, int column)
56104
{
57105
var source = GenerateCode.ObjectStatement(sourceAssertion);
58106
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
@@ -61,7 +109,7 @@ private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion)
61109
Message = ShouldEqualsAnalyzer.Message,
62110
Locations = new DiagnosticResultLocation[]
63111
{
64-
new DiagnosticResultLocation("Test0.cs", 10,13)
112+
new DiagnosticResultLocation("Test0.cs", line, column)
65113
},
66114
Severity = DiagnosticSeverity.Info
67115
});

src/FluentAssertions.Analyzers/Utilities/FluentAssertionsAnalyzer.cs

+13-22
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,26 @@ public sealed override void Initialize(AnalysisContext context)
2323
{
2424
context.EnableConcurrentExecution();
2525
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
26-
context.RegisterCodeBlockAction(AnalyzeCodeBlock);
26+
context.RegisterSyntaxNodeAction(AnalyzeExpressionStatementSyntax, SyntaxKind.ExpressionStatement, SyntaxKind.ArrowExpressionClause);
2727
}
2828

29-
private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
29+
private void AnalyzeExpressionStatementSyntax(SyntaxNodeAnalysisContext context)
3030
{
31-
var method = context.CodeBlock as MethodDeclarationSyntax;
32-
if (method == null) return;
33-
34-
if (!ShouldAnalyzeMethod(method)) return;
35-
36-
if (method.Body != null)
31+
ExpressionSyntax expression = context.Node.Kind() switch
32+
{
33+
SyntaxKind.ExpressionStatement => ((ExpressionStatementSyntax)context.Node).Expression,
34+
SyntaxKind.ArrowExpressionClause => ((ArrowExpressionClauseSyntax)context.Node).Expression,
35+
_ => null
36+
};
37+
if (expression == null)
3738
{
38-
foreach (var statement in method.Body.Statements.OfType<ExpressionStatementSyntax>())
39-
{
40-
var diagnostic = AnalyzeExpressionSafely(statement.Expression, context.SemanticModel);
41-
if (diagnostic != null)
42-
{
43-
context.ReportDiagnostic(diagnostic);
44-
}
45-
}
4639
return;
4740
}
48-
if (method.ExpressionBody != null)
41+
42+
var diagnostic = AnalyzeExpressionSafely(expression, context.SemanticModel);
43+
if (diagnostic != null)
4944
{
50-
var diagnostic = AnalyzeExpressionSafely(method.ExpressionBody.Expression, context.SemanticModel);
51-
if (diagnostic != null)
52-
{
53-
context.ReportDiagnostic(diagnostic);
54-
}
45+
context.ReportDiagnostic(diagnostic);
5546
}
5647
}
5748

0 commit comments

Comments
 (0)