Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxNodeExtensions: increase GetBody coverage #8639

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using FluentAssertions.Extensions;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Text;
using SonarAnalyzer.CFG.Roslyn;
Expand Down Expand Up @@ -172,6 +173,40 @@ public void ArrowExpressionBody_WithNotExpectedNode_ReturnsNull()
public void GetDeclarationTypeName_Struct() =>
ExtensionsCS.GetDeclarationTypeName(SyntaxFactory.StructDeclaration("MyStruct")).Should().Be("struct");

[TestMethod]
public void GetBody_FieldDeclaration()
{
ExtensionsCS.GetBody(SyntaxFactory.FieldDeclaration(SyntaxFactory.VariableDeclaration(SyntaxFactory.ParseTypeName("int")))).Should().BeNull();
}

[TestMethod]
public void GetBody_PropertyDeclaration_SingleAccessor()
{
var code = "class AClass { int AProperty { get => 42; } }";
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<PropertyDeclarationSyntax>()).Should().BeNull();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<T> is repeated very often. Extract a helper

BlockSyntax ParseForBlockSyntax<T>(string code) where T: CSharpSyntaxNode
    => ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<T>());

}

[TestMethod]
public void GetBody_PropertyDeclaration_MultipleAccessors()
{
var code = "class AClass { int AProperty { get => 42; set { } } }";
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<PropertyDeclarationSyntax>()).Should().BeNull();
}

[TestMethod]
public void GetBody_AccessorDeclaration_ArrowExpression()
{
var code = "class AClass { int AProperty { get => 42; } }";
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<AccessorDeclarationSyntax>()).Should().BeNull();
}

[TestMethod]
public void GetBody_AccessorDeclaration_BodyBlock()
{
var code = "class AClass { int AProperty { set { } } }";
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<AccessorDeclarationSyntax>()).Should().NotBeNull();
}
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

[TestMethod]
public void CreateCfg_MethodDeclaration_ReturnsCfg_CS()
{
Expand Down Expand Up @@ -230,7 +265,7 @@ public void CreateCfg_FieldInitializerWithoutOperation_ReturnsCfg_CS()
const string code = """
public class Sample
{
private string field = null!; // null! itself doens't have operation, and we can still generate CFG for it from the equals clause
private string field = null!; // null! itself doesn't have operation, and we can still generate CFG for it from the equals clause
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have an operation

}
""";
CreateCfgCS<SyntaxCS.EqualsValueClauseSyntax>(code).Should().NotBeNull();
Expand Down