-
Notifications
You must be signed in to change notification settings - Fork 228
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
SE: Add IInvocationOperationExtensions.Target
tests
#9513
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bd9ddab
Add GetInstance tetsts and use the GetInstance method where it makes …
mary-georgiou-sonarsource faf9464
Empty-Commit for CI
mary-georgiou-sonarsource e673c54
apply comments
mary-georgiou-sonarsource 09a5a4a
call instance property as method makes no sense in VB context
mary-georgiou-sonarsource 1c95755
Rename extension method
mary-georgiou-sonarsource 5627143
add UT
mary-georgiou-sonarsource e2cb15f
cleanup
mary-georgiou-sonarsource 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
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
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
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
101 changes: 101 additions & 0 deletions
101
...s/tests/SonarAnalyzer.Test/SymbolicExecution/Roslyn/IInvocationOperationExtensionsTest.cs
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using SonarAnalyzer.SymbolicExecution.Roslyn; | ||
using StyleCop.Analyzers.Lightup; | ||
using SyntaxCS = Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using SyntaxVB = Microsoft.CodeAnalysis.VisualBasic.Syntax; | ||
|
||
namespace SonarAnalyzer.Test.SymbolicExecution.Roslyn; | ||
|
||
[TestClass] | ||
public class IInvocationOperationExtensionsTest | ||
{ | ||
[DataTestMethod] | ||
[DataRow("sample.Method()", OperationKind.FieldReference)] | ||
[DataRow("this.Method()", OperationKind.InstanceReference)] | ||
[DataRow("sample.ExtensionMethod()", OperationKind.FieldReference)] | ||
[DataRow("sample.GetSample().Method()", OperationKind.Invocation)] | ||
|
||
public void Invocation_GetInstance_ReturnsSymbol_CS(string invocation, OperationKind kind) | ||
{ | ||
var code = $$""" | ||
|
||
public class Sample | ||
{ | ||
Sample sample = new Sample(); | ||
|
||
public void Method() {} | ||
|
||
void M() => {{invocation}}; | ||
|
||
public Sample GetSample() => new Sample(); | ||
} | ||
|
||
public static class Extensions | ||
{ | ||
public static void ExtensionMethod(this Sample sample) {} | ||
} | ||
"""; | ||
var (tree, model) = TestHelper.CompileCS(code); | ||
var invocationSyntax = tree.GetRoot().DescendantNodesAndSelf().OfType<SyntaxCS.InvocationExpressionSyntax>().First(); | ||
var operation = IInvocationOperationWrapper.FromOperation(model.GetOperation(invocationSyntax)); | ||
operation.Target(ProgramState.Empty).Should().NotBeNull().And.BeAssignableTo<IOperation>().Which.Kind.Should().Be(kind); | ||
} | ||
|
||
[DataTestMethod] | ||
[DataRow("sample.Method()", OperationKind.FieldReference)] | ||
[DataRow("Me.Method()", OperationKind.InstanceReference)] | ||
[DataRow("sample.ExtensionMethod()", OperationKind.FieldReference)] | ||
[DataRow("sample.GetSample().Method()", OperationKind.Invocation)] | ||
|
||
public void Invocation_GetInstance_ReturnsSymbol_VB(string invocation, OperationKind kind) | ||
{ | ||
var code = $$""" | ||
Public Class Sample | ||
Private sample As Sample = New Sample() | ||
|
||
Public Sub Method() | ||
End Sub | ||
|
||
Private Sub M() | ||
{{invocation}} | ||
End Sub | ||
|
||
Public Function GetSample() As Sample | ||
Return New Sample() | ||
End Function | ||
|
||
End Class | ||
|
||
Public Module Extensions | ||
|
||
<System.Runtime.CompilerServices.Extension()> | ||
Public Sub ExtensionMethod(sample As Sample) | ||
End Sub | ||
|
||
End Module | ||
"""; | ||
var (tree, model) = TestHelper.CompileVB(code); | ||
var invocationSyntax = tree.GetRoot().DescendantNodesAndSelf().OfType<SyntaxVB.InvocationExpressionSyntax>().First(); | ||
var operation = IInvocationOperationWrapper.FromOperation(model.GetOperation(invocationSyntax)); | ||
operation.Target(ProgramState.Empty).Should().NotBeNull().And.BeAssignableTo<IOperation>().Which.Kind.Should().Be(kind); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -711,7 +711,8 @@ public void Invocation_CollectionMethods_SetCollectionConstraint() | |
list.RemoveAt(0); | ||
tag = "remove"; | ||
|
||
void Invoke(Action<int> action) { } | ||
list.Add(1, 2); // Extension method | ||
tag = "addExtension"; | ||
"""; | ||
var validator = SETestContext.CreateCS(code, "List<int> list", new PreserveTestCheck("list")).Validator; | ||
validator.ValidateContainsOperation(OperationKind.Invocation); | ||
|
@@ -720,6 +721,7 @@ void Invoke(Action<int> action) { } | |
Verify("clear", ObjectConstraint.NotNull, CollectionConstraint.Empty); | ||
Verify("add", ObjectConstraint.NotNull, CollectionConstraint.NotEmpty); | ||
Verify("remove", ObjectConstraint.NotNull); | ||
Verify("addExtension", ObjectConstraint.NotNull, CollectionConstraint.NotEmpty); | ||
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. this was not adding notempty before that change in the collectionTracker that I missed adding a UT for. |
||
|
||
void Verify(string state, params SymbolicConstraint[] constraints) => | ||
validator.TagValue(state, "list").Should().HaveOnlyConstraints(constraints); | ||
|
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
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
Oops, something went wrong.
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.
I added a test with a fake extension method 😢