Skip to content

Commit

Permalink
use RegisterSyntaxNodeAction instead of RegisterCodeBlockAction (#167)
Browse files Browse the repository at this point in the history
* use RegisterSyntaxNodeAction instead of RegisterCodeBlockAction

* test: nested asserts inside code blocks

* test: actual is method invocation

---------

Co-authored-by: Meir Blachman <meblachm@microsoft.com>
  • Loading branch information
Meir017 and Meir017-msft authored May 27, 2023
1 parent 3eb6906 commit f7212c1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
52 changes: 50 additions & 2 deletions src/FluentAssertions.Analyzers.Tests/Tips/ShouldEqualsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,53 @@ public void ShouldEquals_ShouldBe_ObjectType_TestCodeFix()
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
}

[TestMethod]
[Implemented]
public void ShouldEquals_NestedInsideIfBlock_TestAnalyzer()
=> VerifyCSharpDiagnosticExpressionBody("if(true) { actual.Should().Equals(expected); }", 10, 24);

[TestMethod]
[Implemented]
public void ShouldEquals_NestedInsideIfBlock_ShouldBe_ObjectType_TestCodeFix()
{
var oldSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Equals(expected); }");
var newSource = GenerateCode.ObjectStatement("if(true) { actual.Should().Be(expected); }");

DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
}

[TestMethod]
[Implemented]
public void ShouldEquals_NestedInsideWhileBlock_TestAnalyzer()
=> VerifyCSharpDiagnosticExpressionBody("while(true) { actual.Should().Equals(expected); }", 10, 27);

[TestMethod]
[Implemented]
public void ShouldEquals_NestedInsideWhileBlock_ShouldBe_ObjectType_TestCodeFix()
{
var oldSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Equals(expected); }");
var newSource = GenerateCode.ObjectStatement("while(true) { actual.Should().Be(expected); }");

DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
}

[TestMethod]
[Implemented]
public void ShouldEquals_ActualIsMethodInvoaction_TestAnalyzer()
=> VerifyCSharpDiagnosticExpressionBody("object ResultSupplier() { return null; } \n"
+ "ResultSupplier().Should().Equals(expected);", 11, 0);

[TestMethod]
[Implemented]
public void ShouldEquals_ActualIsMethodInvoaction_ShouldBe_ObjectType_TestCodeFix()
{
const string methodInvocation = "object ResultSupplier() { return null; } \n";
var oldSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Equals(expected);");
var newSource = GenerateCode.ObjectStatement(methodInvocation + "ResultSupplier().Should().Be(expected);");

DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
}

[TestMethod]
[Implemented]
public void ShouldEquals_ShouldBe_NumberType_TestCodeFix()
Expand Down Expand Up @@ -52,7 +99,8 @@ public void ShouldEquals_ShouldEqual_EnumerableType_TestCodeFix()
DiagnosticVerifier.VerifyCSharpFix<ShouldEqualsCodeFix, ShouldEqualsAnalyzer>(oldSource, newSource);
}

private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion)
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion) => VerifyCSharpDiagnosticExpressionBody(sourceAssertion, 10, 13);
private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion, int line, int column)
{
var source = GenerateCode.ObjectStatement(sourceAssertion);
DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
Expand All @@ -61,7 +109,7 @@ private void VerifyCSharpDiagnosticExpressionBody(string sourceAssertion)
Message = ShouldEqualsAnalyzer.Message,
Locations = new DiagnosticResultLocation[]
{
new DiagnosticResultLocation("Test0.cs", 10,13)
new DiagnosticResultLocation("Test0.cs", line, column)
},
Severity = DiagnosticSeverity.Info
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,26 @@ public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCodeBlockAction(AnalyzeCodeBlock);
context.RegisterSyntaxNodeAction(AnalyzeExpressionStatementSyntax, SyntaxKind.ExpressionStatement, SyntaxKind.ArrowExpressionClause);
}

private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
private void AnalyzeExpressionStatementSyntax(SyntaxNodeAnalysisContext context)
{
var method = context.CodeBlock as MethodDeclarationSyntax;
if (method == null) return;

if (!ShouldAnalyzeMethod(method)) return;

if (method.Body != null)
ExpressionSyntax expression = context.Node.Kind() switch
{
SyntaxKind.ExpressionStatement => ((ExpressionStatementSyntax)context.Node).Expression,
SyntaxKind.ArrowExpressionClause => ((ArrowExpressionClauseSyntax)context.Node).Expression,
_ => null
};
if (expression == null)
{
foreach (var statement in method.Body.Statements.OfType<ExpressionStatementSyntax>())
{
var diagnostic = AnalyzeExpressionSafely(statement.Expression, context.SemanticModel);
if (diagnostic != null)
{
context.ReportDiagnostic(diagnostic);
}
}
return;
}
if (method.ExpressionBody != null)

var diagnostic = AnalyzeExpressionSafely(expression, context.SemanticModel);
if (diagnostic != null)
{
var diagnostic = AnalyzeExpressionSafely(method.ExpressionBody.Expression, context.SemanticModel);
if (diagnostic != null)
{
context.ReportDiagnostic(diagnostic);
}
context.ReportDiagnostic(diagnostic);
}
}

Expand Down

0 comments on commit f7212c1

Please sign in to comment.