提交 aec80d76 编写于 作者: G Gen Lu

Rewrite tests for IArgument using updated test generator

上级 be389f36
......@@ -11,72 +11,107 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
<Fact()>
Public Sub ExplicitSimpleArgument()
Dim source = <![CDATA[
Module P
Class P
Sub M1()
M2(1, "")'BIND:"M2(1, "")"
M2(1, 2.0)'BIND:"M2(1, 2.0)"
End Sub
Sub M2(x As Integer, y As String)
Sub M2(x As Integer, y As Double)
End Sub
End Module]]>.Value
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression (static Sub P.M2(x As System.Int32, y As System.String)) (OperationKind.InvocationExpression, Type: System.Void)
IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.String, Constant: )
IInvocationExpression ( Sub P.M2(x As System.Int32, y As System.Double)) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(1, 2.0)')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument) (Syntax: '2.0')
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Double, Constant: 2) (Syntax: '2.0')
]]>.Value
VerifyOperationTreeForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree)
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
<Fact()>
Public Sub ExplicitSimpleArgumentByName()
Dim source = <![CDATA[
Dim source = <![CDATA[
Class P
Sub M1()
M2(y:=1, x:=2.0)'BIND:"M2(y:=1, x:=2.0)"
End Sub
Sub M2(x As Integer, y As Double)
End Sub
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression ( Sub P.M2(x As System.Int32, y As System.Double)) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(y:=1, x:=2.0)')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: '2.0')
IConversionExpression (ConversionKind.Basic, Implicit) (OperationKind.ConversionExpression, Type: System.Int32, Constant: 2) (Syntax: '2.0')
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Double, Constant: 2) (Syntax: '2.0')
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument) (Syntax: '1')
IConversionExpression (ConversionKind.Basic, Implicit) (OperationKind.ConversionExpression, Type: System.Double, Constant: 1) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
]]>.Value
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
<Fact()>
Public Sub XXXX()
Dim source = <![CDATA[
Class P
Sub M1()
M2(y:=1, x:=2)'BIND:"M2(y:=1, x:=2)"
M2(y:=2)'BIND:"M2(y:=2)"
End Sub
Sub M2(x As Integer, y As Integer)
Sub M2(Optional x As Integer = 0, Optional y As Integer = 0)
End Sub
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression ( Sub P.M2(x As System.Int32, y As System.Int32)) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument)
ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2)
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IInvocationExpression ( Sub P.M2([x As System.Int32 = 0], [y As System.Int32 = 0])) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(y:=2)')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: 'M2')
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 0) (Syntax: 'M2')
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument) (Syntax: '2')
ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: '2')
]]>.Value
VerifyOperationTreeForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree)
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
<Fact()>
Public Sub DefaultArgument()
Dim source = <![CDATA[
Dim source = <![CDATA[
Class P
Sub M1()
M2(1)'BIND:"M2(1)"
End Sub
Sub M2(x As Integer, Optional y As Integer = 10)
Sub M2(x As Integer, Optional y As Double = 0.0)
End Sub
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression ( Sub P.M2(x As System.Int32, [y As System.Int32 = 10])) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument)
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 10)
IInvocationExpression ( Sub P.M2(x As System.Int32, [y As System.Double = 0])) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(1)')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
IArgument (ArgumentKind.Positional Matching Parameter: y) (OperationKind.Argument) (Syntax: 'M2')
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Double, Constant: 0) (Syntax: 'M2')
]]>.Value
VerifyOperationTreeForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree)
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
<Fact()>
......@@ -87,27 +122,57 @@ Class P
M2(1, 2, 3)'BIND:"M2(1, 2, 3)"
End Sub
Sub M2(a As Integer, ParamArray c As Integer())
Sub M2(x As Integer, ParamArray array As Integer())
End Sub
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression ( Sub P.M2(a As System.Int32, ParamArray c As System.Int32())) (OperationKind.InvocationExpression, Type: System.Void)
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P)
IArgument (ArgumentKind.Positional Matching Parameter: a) (OperationKind.Argument)
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1)
IArgument (ArgumentKind.ParamArray Matching Parameter: c) (OperationKind.Argument)
IArrayCreationExpression (Dimension sizes: 1, Element Type: System.Int32) (OperationKind.ArrayCreationExpression, Type: System.Int32())
ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2)
IArrayInitializer (OperationKind.ArrayInitializer)
ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2)
ILiteralExpression (Text: 3) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 3)
IInvocationExpression ( Sub P.M2(x As System.Int32, ParamArray array As System.Int32())) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(1, 2, 3)')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
IArgument (ArgumentKind.ParamArray Matching Parameter: array) (OperationKind.Argument) (Syntax: 'M2(1, 2, 3)')
IArrayCreationExpression (Element Type: System.Int32) (OperationKind.ArrayCreationExpression, Type: System.Int32()) (Syntax: 'M2(1, 2, 3)')
Dimension Sizes(1): ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: 'M2(1, 2, 3)')
Initializer: IArrayInitializer (OperationKind.ArrayInitializer) (Syntax: 'M2(1, 2, 3)')
Element Values(2): ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: '2')
ILiteralExpression (Text: 3) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 3) (Syntax: '3')
]]>.Value
VerifyOperationTreeForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree)
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
<Fact()>
Public Sub ArrayAsParamsArrayArgument()
Dim source = <![CDATA[
Class P
Sub M1()
M2(1, New Integer() {2, 3})'BIND:"M2(1, New Integer() {2, 3})"
End Sub
Sub M2(x As Integer, ParamArray array As Integer())
End Sub
End Class]]>.Value
Dim expectedOperationTree = <![CDATA[
IInvocationExpression ( Sub P.M2(x As System.Int32, ParamArray array As System.Int32())) (OperationKind.InvocationExpression, Type: System.Void) (Syntax: 'M2(1, New I ... r() {2, 3})')
Instance Receiver: IInstanceReferenceExpression (InstanceReferenceKind.Implicit) (OperationKind.InstanceReferenceExpression, Type: P) (Syntax: 'M2')
Arguments(2): IArgument (ArgumentKind.Positional Matching Parameter: x) (OperationKind.Argument) (Syntax: '1')
ILiteralExpression (Text: 1) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 1) (Syntax: '1')
IArgument (ArgumentKind.ParamArray Matching Parameter: array) (OperationKind.Argument) (Syntax: 'New Integer() {2, 3}')
IArrayCreationExpression (Element Type: System.Int32) (OperationKind.ArrayCreationExpression, Type: System.Int32()) (Syntax: 'New Integer() {2, 3}')
Dimension Sizes(1): ILiteralExpression (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: 'New Integer() {2, 3}')
Initializer: IArrayInitializer (OperationKind.ArrayInitializer) (Syntax: '{2, 3}')
Element Values(2): ILiteralExpression (Text: 2) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 2) (Syntax: '2')
ILiteralExpression (Text: 3) (OperationKind.LiteralExpression, Type: System.Int32, Constant: 3) (Syntax: '3')
]]>.Value
Dim expectedDiagnostics = String.Empty
VerifyOperationTreeAndDiagnosticsForTest(Of InvocationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics)
End Sub
End Class
End Namespace
......
......@@ -139,8 +139,8 @@ private bool IsIfNullCheck(IOperation statement, IParameterSymbol parameter)
private bool IsStringCheck(IOperation condition, IParameterSymbol parameter)
{
if (condition is IInvocationExpression invocation &&
invocation.ArgumentsInSourceOrder.Length == 1 &&
IsParameterReference(invocation.ArgumentsInSourceOrder[0].Value, parameter))
invocation.ArgumentsInEvaluationOrder.Length == 1 &&
IsParameterReference(invocation.ArgumentsInEvaluationOrder[0].Value, parameter))
{
var targetMethod = invocation.TargetMethod;
if (targetMethod?.Name == nameof(string.IsNullOrEmpty) ||
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.Semantics;
using Xunit;
......@@ -204,18 +205,8 @@ public override void VisitInvocationExpression(IInvocationExpression operation)
{
var targetMethod = operation.TargetMethod;
var isVirtual = operation.IsVirtual;
foreach (var argument in operation.ArgumentsInEvaluationOrder)
{
Visit(argument);
}
if (targetMethod != null)
{
foreach (var parameter in targetMethod.Parameters)
{
var matchingArgument = operation.GetArgumentMatchingParameter(parameter);
Visit(matchingArgument);
}
}
// base.VisitInvocationExpression only visits operations in ArgumentsInEvaluationOrder.
VisitArgumentsInParameterOrder(operation, targetMethod?.Parameters);
base.VisitInvocationExpression(operation);
}
......@@ -331,14 +322,8 @@ public override void VisitIndexedPropertyReferenceExpression(IIndexedPropertyRef
{
var member = operation.Member;
var property = operation.Property;
if (property != null)
{
foreach (var parameter in property.Parameters)
{
var matchingArgument = operation.GetArgumentMatchingParameter(parameter);
Visit(matchingArgument);
}
}
// base.VisitPropertyReferenceExpression only visits operations in ArgumentsInEvaluationOrder.
VisitArgumentsInParameterOrder(operation, property?.Parameters);
base.VisitIndexedPropertyReferenceExpression(operation);
}
......@@ -429,14 +414,8 @@ public override void VisitAddressOfExpression(IAddressOfExpression operation)
public override void VisitObjectCreationExpression(IObjectCreationExpression operation)
{
var ctor = operation.Constructor;
if (ctor != null)
{
foreach (var parameter in ctor.Parameters)
{
var matchingArgument = operation.GetArgumentMatchingParameter(parameter);
Visit(matchingArgument);
}
}
// base.VisitObjectCreationExpression only visits operations in ArgumentsInEvaluationOrder.
VisitArgumentsInParameterOrder(operation, ctor?.Parameters);
base.VisitObjectCreationExpression(operation);
}
......@@ -536,5 +515,22 @@ public override void VisitInvalidExpression(IInvalidExpression operation)
{
base.VisitInvalidExpression(operation);
}
private void VisitArgumentsInParameterOrder(IHasArgumentsExpression operation, ImmutableArray<IParameterSymbol>? parameters)
{
foreach (var argument in operation.ArgumentsInParameterOrder())
{
Visit(argument);
}
if (parameters.HasValue)
{
foreach (var parameter in parameters.Value)
{
var matchingArgument = operation.GetArgumentMatchingParameter(parameter);
Visit(matchingArgument);
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册