提交 9d772785 编写于 作者: M Manish Vasani

Add helper methods to Verify operation tree for annonated text within a source file.

These APIs will be used in autogenerated unit test for Operation tree verification.
上级 ff6bed5f
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
namespace Microsoft.CodeAnalysis.CSharp.UnitTests namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{ {
public partial class IOperationTests : CompilingTestBase public partial class IOperationTests : SemanticModelTestBase
{ {
[Fact] [Fact]
[WorkItem(382240, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=382240")] [WorkItem(382240, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=382240")]
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
namespace Microsoft.CodeAnalysis.CSharp.UnitTests namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{ {
public partial class IOperationTests : CompilingTestBase public partial class IOperationTests : SemanticModelTestBase
{ {
[Fact, WorkItem(17595, "https://github.com/dotnet/roslyn/issues/17595")] [Fact, WorkItem(17595, "https://github.com/dotnet/roslyn/issues/17595")]
public void NoInitializers() public void NoInitializers()
......
...@@ -6,9 +6,8 @@ ...@@ -6,9 +6,8 @@
using Microsoft.CodeAnalysis.CSharp.Symbols; using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities; using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Text; using Microsoft.CodeAnalysis.Test.Utilities;
using Xunit; using Xunit;
using Roslyn.Test.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{ {
...@@ -220,5 +219,41 @@ protected CompilationUtils.SemanticInfoSummary GetSemanticInfoForTest(string tes ...@@ -220,5 +219,41 @@ protected CompilationUtils.SemanticInfoSummary GetSemanticInfoForTest(string tes
{ {
return GetSemanticInfoForTest<ExpressionSyntax>(testSrc); return GetSemanticInfoForTest<ExpressionSyntax>(testSrc);
} }
protected string GetOperationTreeForTest<TSyntaxNode>(CSharpCompilation compilation)
where TSyntaxNode: SyntaxNode
{
var tree = compilation.SyntaxTrees[0];
var model = compilation.GetSemanticModel(tree);
SyntaxNode syntaxNode = GetSyntaxNodeOfTypeForBinding<TSyntaxNode>(GetSyntaxNodeList(tree));
if (syntaxNode == null)
{
return null;
}
var operation = model.GetOperationInternal(syntaxNode);
return operation != null ? OperationTreeVerifier.GetOperationTree(operation) : null;
}
protected string GetOperationTreeForTest<TSyntaxNode>(string testSrc, string expectedOperationTree, CSharpParseOptions parseOptions = null)
where TSyntaxNode : SyntaxNode
{
var compilation = CreateCompilationWithMscorlib(testSrc, new[] { SystemCoreRef }, parseOptions: parseOptions);
return GetOperationTreeForTest<TSyntaxNode>(compilation);
}
protected void VerifyOperationTreeForTest<TSyntaxNode>(CSharpCompilation compilation, string expectedOperationTree)
where TSyntaxNode : SyntaxNode
{
var actualOperationTree = GetOperationTreeForTest<TSyntaxNode>(compilation);
OperationTreeVerifier.Verify(expectedOperationTree, actualOperationTree);
}
protected void VerifyOperationTreeForTest<TSyntaxNode>(string testSrc, string expectedOperationTree, CSharpParseOptions parseOptions = null)
where TSyntaxNode : SyntaxNode
{
var actualOperationTree = GetOperationTreeForTest<TSyntaxNode>(testSrc, expectedOperationTree, parseOptions);
OperationTreeVerifier.Verify(expectedOperationTree, actualOperationTree);
}
} }
} }
' 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.
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
...@@ -151,4 +152,32 @@ Public MustInherit Class SemanticModelTestBase : Inherits BasicTestBase ...@@ -151,4 +152,32 @@ Public MustInherit Class SemanticModelTestBase : Inherits BasicTestBase
).Where(Function(s) anyArity OrElse DirectCast(s, Symbol).GetArity() = arity.Value).ToList() ).Where(Function(s) anyArity OrElse DirectCast(s, Symbol).GetArity() = arity.Value).ToList()
End Function End Function
Friend Function GetOperationTreeForTest(Of TSyntaxNode As SyntaxNode)(compilation As VisualBasicCompilation, fileName As String, Optional which As Integer = 0) As String
Dim node As SyntaxNode = CompilationUtils.FindBindingText(Of TSyntaxNode)(compilation, fileName, which)
If node Is Nothing Then
Return 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)
Return If(operation IsNot Nothing, OperationTreeVerifier.GetOperationTree(operation), Nothing)
End Function
Friend Function GetOperationTreeForTest(Of TSyntaxNode As SyntaxNode)(testSrc As String, Optional parseOptions As VisualBasicParseOptions = Nothing, Optional which As Integer = 0) As String
Dim fileName = "a.vb"
Dim syntaxTree = Parse(testSrc, fileName, parseOptions)
Dim compilation = CreateCompilationWithMscorlib(syntaxTree)
Return GetOperationTreeForTest(Of TSyntaxNode)(compilation, fileName, which)
End Function
Friend Sub VerifyOperationTreeForTest(Of TSyntaxNode As SyntaxNode)(compilation As VisualBasicCompilation, fileName As String, expectedOperationTree As String, Optional which As Integer = 0)
Dim actualOperationTree = GetOperationTreeForTest(Of TSyntaxNode)(compilation, fileName, which)
OperationTreeVerifier.Verify(expectedOperationTree, actualOperationTree)
End Sub
Friend Sub VerifyOperationTreeForTest(Of TSyntaxNode As SyntaxNode)(testSrc As String, expectedOperationTree As String, Optional parseOptions As VisualBasicParseOptions = Nothing, Optional which As Integer = 0)
Dim actualOperationTree = GetOperationTreeForTest(Of TSyntaxNode)(testSrc, parseOptions, which)
OperationTreeVerifier.Verify(expectedOperationTree, actualOperationTree)
End Sub
End Class End Class
...@@ -8,7 +8,7 @@ Imports Roslyn.Test.Utilities ...@@ -8,7 +8,7 @@ Imports Roslyn.Test.Utilities
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
Partial Public Class IOperationTests Partial Public Class IOperationTests
Inherits BasicTestBase Inherits SemanticModelTestBase
<Fact> <Fact>
Public Sub InvalidUserDefinedOperators() Public Sub InvalidUserDefinedOperators()
......
...@@ -8,7 +8,7 @@ Imports Roslyn.Test.Utilities ...@@ -8,7 +8,7 @@ Imports Roslyn.Test.Utilities
Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Semantics
Partial Public Class IOperationTests Partial Public Class IOperationTests
Inherits BasicTestBase Inherits SemanticModelTestBase
<Fact, WorkItem(17595, "https://github.com/dotnet/roslyn/issues/17595")> <Fact, WorkItem(17595, "https://github.com/dotnet/roslyn/issues/17595")>
Public Sub NoInitializers() Public Sub NoInitializers()
......
...@@ -159,7 +159,7 @@ internal static void VerifyOperationTree(this Compilation compilation, SyntaxNod ...@@ -159,7 +159,7 @@ internal static void VerifyOperationTree(this Compilation compilation, SyntaxNod
var actualTextBuilder = new StringBuilder(); var actualTextBuilder = new StringBuilder();
SemanticModel model = compilation.GetSemanticModel(node.SyntaxTree); SemanticModel model = compilation.GetSemanticModel(node.SyntaxTree);
AppendOperationTree(model, node, actualTextBuilder); AppendOperationTree(model, node, actualTextBuilder);
VerifyOperationTree(expectedOperationTree, actualTextBuilder.ToString()); OperationTreeVerifier.Verify(expectedOperationTree, actualTextBuilder.ToString());
} }
internal static void VerifyOperationTree(this Compilation compilation, string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false) internal static void VerifyOperationTree(this Compilation compilation, string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false)
...@@ -218,7 +218,7 @@ internal static void VerifyOperationTree(this Compilation compilation, string sy ...@@ -218,7 +218,7 @@ internal static void VerifyOperationTree(this Compilation compilation, string sy
actualTextBuilder.Append(Environment.NewLine); actualTextBuilder.Append(Environment.NewLine);
} }
VerifyOperationTree(expectedOperationTree, actualTextBuilder.ToString()); OperationTreeVerifier.Verify(expectedOperationTree, actualTextBuilder.ToString());
} }
private static void AppendOperationTree(SemanticModel model, SyntaxNode node, StringBuilder actualTextBuilder, int initialIndent = 0) private static void AppendOperationTree(SemanticModel model, SyntaxNode node, StringBuilder actualTextBuilder, int initialIndent = 0)
...@@ -235,15 +235,6 @@ private static void AppendOperationTree(SemanticModel model, SyntaxNode node, St ...@@ -235,15 +235,6 @@ private static void AppendOperationTree(SemanticModel model, SyntaxNode node, St
} }
} }
private static void VerifyOperationTree(string expectedOperationTree, string actualOperationTree)
{
char[] newLineChars = Environment.NewLine.ToCharArray();
string actual = actualOperationTree.Trim(newLineChars);
expectedOperationTree = expectedOperationTree.Trim(newLineChars);
expectedOperationTree = Regex.Replace(expectedOperationTree, "([^\r])\n", "$1" + Environment.NewLine);
AssertEx.AreEqual(expectedOperationTree, actual);
}
internal static bool CanHaveExecutableCodeBlock(ISymbol symbol) internal static bool CanHaveExecutableCodeBlock(ISymbol symbol)
{ {
switch (symbol.Kind) switch (symbol.Kind)
......
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis.Semantics; using Microsoft.CodeAnalysis.Semantics;
using Microsoft.CodeAnalysis.Test.Extensions; using Microsoft.CodeAnalysis.Test.Extensions;
using Roslyn.Test.Utilities;
using Xunit; using Xunit;
namespace Microsoft.CodeAnalysis.Test.Utilities namespace Microsoft.CodeAnalysis.Test.Utilities
...@@ -42,6 +44,15 @@ public static string GetOperationTree(IOperation operation, int initialIndent = ...@@ -42,6 +44,15 @@ public static string GetOperationTree(IOperation operation, int initialIndent =
return walker._builder.ToString(); return walker._builder.ToString();
} }
public static void Verify(string expectedOperationTree, string actualOperationTree)
{
char[] newLineChars = Environment.NewLine.ToCharArray();
string actual = actualOperationTree.Trim(newLineChars);
expectedOperationTree = expectedOperationTree.Trim(newLineChars);
expectedOperationTree = Regex.Replace(expectedOperationTree, "([^\r])\n", "$1" + Environment.NewLine);
AssertEx.AreEqual(expectedOperationTree, actual);
}
#region Logging helpers #region Logging helpers
private void LogCommonPropertiesAndNewLine(IOperation operation) private void LogCommonPropertiesAndNewLine(IOperation operation)
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.CodeAnalysis.Test.Extensions
{
public static class SemanticModelExtensions
{
public static IOperation GetOperationInternal(this SemanticModel model, SyntaxNode node)
{
return model.GetOperationInternal(node);
}
}
}
...@@ -129,6 +129,7 @@ ...@@ -129,6 +129,7 @@
<Compile Include="Diagnostics\TrackingDiagnosticAnalyzer.cs" /> <Compile Include="Diagnostics\TrackingDiagnosticAnalyzer.cs" />
<Compile Include="ExceptionHelper.cs" /> <Compile Include="ExceptionHelper.cs" />
<Compile Include="CompilerTraitAttribute.cs" /> <Compile Include="CompilerTraitAttribute.cs" />
<Compile Include="Extensions\SemanticModelExtensions.cs" />
<Compile Include="Extensions\SymbolExtensions.cs" /> <Compile Include="Extensions\SymbolExtensions.cs" />
<Compile Include="FX\ConsoleOutput.cs" /> <Compile Include="FX\ConsoleOutput.cs" />
<Compile Include="FX\CultureHelpers.cs" /> <Compile Include="FX\CultureHelpers.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册