diff --git a/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs b/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs index e365f9368d2..df9553e1b1b 100644 --- a/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs +++ b/analyzers/tests/SonarAnalyzer.Test/Extensions/SyntaxNodeExtensionsTest.cs @@ -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; @@ -172,6 +173,56 @@ public void GetDeclarationTypeName_RecordStruct() => 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()).Should().BeNull(); + } + + [TestMethod] + public void GetBody_PropertyDeclaration_MultipleAccessors() + { + var code = "class AClass { int AProperty { get => 42; set { } } }"; + ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single()).Should().BeNull(); + } + + [TestMethod] + public void GetBody_AccessorDeclaration_ArrowExpression() + { + var code = "class AClass { int AProperty { get => 42; } }"; + ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single()).Should().BeNull(); + } + + [TestMethod] + public void GetBody_AccessorDeclaration_BodyBlock() + { + var code = "class AClass { int AProperty { set { int i = 1; i++; i++; } } }"; + ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single()) + .Should().BeOfType().Which.Statements.Should().HaveCount(3); + } + + [TestMethod] + public void GetBody_LocalFunctionStatement_ArrowExpression() + { + var code = "class AClass { void AMethod() { int ALocalFunction() => 42; } }"; + ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single()).Should().BeNull(); + } + + [TestMethod] + public void GetBody_LocalFunctionStatement_BodyBlock() + { + var code = "class AClass { void AMethod() { int ALocalFunction(int i) { i++; i++; i++; return i; } } }"; + ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single()) + .Should().BeOfType().Which.Statements.Should().HaveCount(4); + } + [TestMethod] public void CreateCfg_MethodDeclaration_ReturnsCfg_CS() { @@ -230,7 +281,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 } """; CreateCfgCS(code).Should().NotBeNull();