-
Notifications
You must be signed in to change notification settings - Fork 231
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
antonioaversa
wants to merge
6
commits into
master
Choose a base branch
from
antonio/syntax-extensions-increase-coverage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4ded29d
SyntaxNodeExtensions: increase GetBody coverage
antonioaversa 9ebc49d
Fix naming
antonioaversa 827f6e0
Add missing UTs in local functions
antonioaversa edc5423
Code review 1
antonioaversa 3d1f73a
Code review 2
antonioaversa 91a5f26
Code review 3
antonioaversa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<PropertyDeclarationSyntax>()).Should().BeNull(); | ||
} | ||
|
||
[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 { var x1; var x2; var x3; } } }"; | ||
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<AccessorDeclarationSyntax>()) | ||
.Should().BeOfType<BlockSyntax>().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<LocalFunctionStatementSyntax>()).Should().BeNull(); | ||
} | ||
|
||
[TestMethod] | ||
public void GetBody_LocalFunctionStatement_BodyBlock() | ||
{ | ||
var code = "class AClass { void AMethod() { int ALocalFunction() { var x1; var x2; var x3; return 42; } } }"; | ||
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ExtensionsCS.GetBody(CSharpSyntaxTree.ParseText(code).Single<LocalFunctionStatementSyntax>()) | ||
.Should().BeOfType<BlockSyntax>().Which.Statements.Should().HaveCount(4); | ||
} | ||
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. have an operation |
||
} | ||
"""; | ||
CreateCfgCS<SyntaxCS.EqualsValueClauseSyntax>(code).Should().NotBeNull(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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