提交 4ab6b899 编写于 作者: G Gen Lu

Add tests for In/OutConversion extension methods

上级 7cef79f9
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// 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;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Semantics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.VisualBasic;
using Roslyn.Test.Utilities;
using Xunit;
......@@ -2219,6 +2221,34 @@ struct G<T>
VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(source, expectedOperationTree, expectedDiagnostics);
}
[CompilerTrait(CompilerFeature.IOperation)]
[Fact]
public void GettingInOutConverionFromCSharpArgumentShouldThrowException()
{
string source = @"
class P
{
static void M1()
{
/*<bind>*/M2(1)/*</bind>*/;
}
static void M2(int x)
{
}
}
";
var compilation = CreateCompilation(source);
var (operation, syntaxNode) = GetOperationAndSyntaxForTest<InvocationExpressionSyntax>(compilation);
var invocation = (IInvocationExpression)operation;
var argument = invocation.ArgumentsInEvaluationOrder[0];
Assert.Throws<ArgumentException>(() => argument.GetInConversion());
Assert.Throws<ArgumentException>(() => argument.GetOutConversion());
}
private class IndexerAccessArgumentVerifier : OperationWalker
{
private readonly Compilation _compilation;
......
......@@ -852,6 +852,24 @@ Public MustInherit Class BasicTestBaseBase
Public Shared Function GetAssertTheseDiagnosticsString(allDiagnostics As ImmutableArray(Of Diagnostic), suppressInfos As Boolean) As String
Return DumpAllDiagnostics(allDiagnostics, suppressInfos)
End Function
Friend Shared Function GetOperationAndSyntaxForTest(Of TSyntaxNode As SyntaxNode)(compilation As Compilation, fileName As String, Optional which As integer = 0) As (operation as IOperation, synxtaxNode As SyntaxNode)
Dim node As SyntaxNode = CompilationUtils.FindBindingText(Of TSyntaxNode)(compilation, fileName, which, prefixMatch:=True)
If node Is Nothing Then
Return (Nothing, Nothing)
End If
Dim tree = (From t In compilation.SyntaxTrees Where t.FilePath = fileName).Single()
Dim semanticModel = compilation.GetSemanticModel(tree)
Dim operation = semanticModel.GetOperationInternal(node)
If operation IsNot Nothing Then
Return (operation, node)
Else
Return (Nothing, Nothing)
End If
End Function
#End Region
End Class
......@@ -2,6 +2,7 @@
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Semantics
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
......@@ -974,7 +975,7 @@ BC33037: Cannot copy the value of 'ByRef' parameter 'a' back to the matching arg
<CompilerTrait(CompilerFeature.IOperation)>
<Fact>
Public Sub TestCloneInOutConversion()
Public Sub GettingInOutConverionFromVBArgument()
Dim source = <![CDATA[
Class C
Public Shared Widening Operator CType(ByVal c As C) As Integer
......@@ -989,23 +990,39 @@ End Class
Class Program
Sub M1()
Dim x = New C()
Dim y = New C()
Dim z = New C()
M2(x, y, z)
M2(x)'BIND:"M2(x)"
End Sub
Sub M2(ByRef a As Integer, ByRef b As Double, ByRef c As C)
Sub M2(ByRef a As Integer)
End Sub
End Class]]>.Value
End Class]]>.Value
Dim fileName = "a.vb"
Dim syntaxTree = Parse(source, fileName, options:=Nothing)
Dim compilation = CreateCompilationWithMscorlib45AndVBRuntime({syntaxTree}, DefaultVbReferences.Concat({ValueTupleRef, SystemRuntimeFacadeRef}))
Dim tree = (From t In compilation.SyntaxTrees Where t.FilePath = fileName).Single()
Dim model = compilation.GetSemanticModel(tree)
VerifyClone(model)
Dim result = GetOperationAndSyntaxForTest(Of InvocationExpressionSyntax)(compilation, fileName)
Dim operatorNodes = compilation.SyntaxTrees(0).GetRoot().DescendantNodes.Where(Function(n As SyntaxNode) As Boolean
return n.Kind = SyntaxKind.OperatorStatement
End Function).ToList()
Dim semanticModel = compilation.GetSemanticModel(compilation.SyntaxTrees(0))
Dim expectedInKind = ConversionKind.Widening Or ConversionKind.UserDefined
Dim exptectedInMethod = CType(semanticModel.GetDeclaredSymbolFromSyntaxNode(operatorNodes(0)), IMethodSymbol)
Dim expectedOutKind = ConversionKind.Narrowing Or ConversionKind.UserDefined
Dim expectedOutMethod = CType(semanticModel.GetDeclaredSymbolFromSyntaxNode(operatorNodes(1)), IMethodSymbol)
Dim invocation = CType(result.operation, IInvocationExpression)
Dim argument = invocation.ArgumentsInEvaluationOrder(0)
Dim inConversion = argument.GetInConversion()
Assert.Same(exptectedInMethod, inConversion.MethodSymbol)
Assert.Equal(expectedInKind, inConversion.Kind)
Dim outConversion = argument.GetOutConversion()
Assert.Same(expectedOutMethod, outConversion.MethodSymbol)
Assert.Equal(expectedOutKind, outConversion.Kind)
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册