Пример #1
0
        internal static void VerifyClone(SemanticModel model)
        {
            foreach (var node in model.SyntaxTree.GetRoot().DescendantNodes())
            {
                var operation = model.GetOperation(node);
                if (operation == null)
                {
                    continue;
                }

                var clonedOperation = model.CloneOperation(operation);

                // check whether cloned IOperation is same as original one
                var original = OperationTreeVerifier.GetOperationTree(model.Compilation, operation);
                var cloned   = OperationTreeVerifier.GetOperationTree(model.Compilation, clonedOperation);

                Assert.Equal(original, cloned);

                // make sure cloned operation is value equal but doesn't share any IOperations
                var originalSet = new HashSet <IOperation>(operation.DescendantsAndSelf());
                var clonedSet   = new HashSet <IOperation>(clonedOperation.DescendantsAndSelf());

                Assert.Equal(originalSet.Count, clonedSet.Count);
                Assert.Equal(0, originalSet.Intersect(clonedSet).Count());
            }
        }
Пример #2
0
        public static string GetOperationTree(IOperation operation, int initialIndent = 0)
        {
            var walker = new OperationTreeVerifier(operation, initialIndent);

            walker.Visit(operation);
            return(walker._builder.ToString());
        }
Пример #3
0
        public static void AppendOperationTree(
            this SemanticModel model,
            SyntaxNode node,
            StringBuilder actualTextBuilder,
            int initialIndent = 0
            )
        {
            IOperation operation = model.GetOperation(node);

            if (operation != null)
            {
                string operationTree = OperationTreeVerifier.GetOperationTree(
                    model.Compilation,
                    operation,
                    initialIndent
                    );
                actualTextBuilder.Append(operationTree);
            }
            else
            {
                actualTextBuilder.Append(
                    $"  SemanticModel.GetOperation() returned NULL for node with text: '{node.ToString()}'"
                    );
            }
        }
Пример #4
0
        public static void VerifyOperationTree(this SemanticModel model, SyntaxNode node, string expectedOperationTree)
        {
            var actualTextBuilder = new StringBuilder();

            AppendOperationTree(model, node, actualTextBuilder);
            OperationTreeVerifier.Verify(expectedOperationTree, actualTextBuilder.ToString());
        }
Пример #5
0
        internal static void VerifyOperationTree(this Compilation compilation, SyntaxNode node, string expectedOperationTree)
        {
            var           actualTextBuilder = new StringBuilder();
            SemanticModel model             = compilation.GetSemanticModel(node.SyntaxTree);

            AppendOperationTree(model, node, actualTextBuilder);
            OperationTreeVerifier.Verify(expectedOperationTree, actualTextBuilder.ToString());
        }
Пример #6
0
        internal static void VerifyOperationTree(this Compilation compilation, string symbolToVerify, string expectedOperationTree, bool skipImplicitlyDeclaredSymbols = false)
        {
            SyntaxTree    tree         = compilation.SyntaxTrees.First();
            SyntaxNode    root         = tree.GetRoot();
            SemanticModel model        = compilation.GetSemanticModel(tree);
            var           declarations = new List <DeclarationInfo>();

            model.ComputeDeclarationsInNode(root, getSymbol: true, builder: declarations, cancellationToken: CancellationToken.None);

            var actualTextBuilder = new StringBuilder();

            foreach (DeclarationInfo declaration in declarations.Where(d => d.DeclaredSymbol != null).OrderBy(d => d.DeclaredSymbol.ToTestDisplayString()))
            {
                if (!CanHaveExecutableCodeBlock(declaration.DeclaredSymbol))
                {
                    continue;
                }

                if (skipImplicitlyDeclaredSymbols && declaration.DeclaredSymbol.IsImplicitlyDeclared)
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(symbolToVerify) && !declaration.DeclaredSymbol.Name.Equals(symbolToVerify, StringComparison.Ordinal))
                {
                    continue;
                }

                actualTextBuilder.Append(declaration.DeclaredSymbol.ToTestDisplayString());

                if (declaration.ExecutableCodeBlocks.Length == 0)
                {
                    actualTextBuilder.Append($" ('0' executable code blocks)");
                }
                else
                {
                    // Workaround for https://github.com/dotnet/roslyn/issues/11903 - skip the IOperation for EndBlockStatement.
                    ImmutableArray <SyntaxNode> executableCodeBlocks = declaration.ExecutableCodeBlocks;
                    if (declaration.DeclaredSymbol.Kind == SymbolKind.Method && compilation.Language == LanguageNames.VisualBasic)
                    {
                        executableCodeBlocks = executableCodeBlocks.RemoveAt(executableCodeBlocks.Length - 1);
                    }

                    foreach (SyntaxNode executableCodeBlock in executableCodeBlocks)
                    {
                        actualTextBuilder.Append(Environment.NewLine);
                        AppendOperationTree(model, executableCodeBlock, actualTextBuilder, initialIndent: 2);
                    }
                }

                actualTextBuilder.Append(Environment.NewLine);
            }

            OperationTreeVerifier.Verify(expectedOperationTree, actualTextBuilder.ToString());
        }
Пример #7
0
        public static void VerifyGraph(Compilation compilation, string expectedFlowGraph, ControlFlowGraph graph)
        {
            var actualFlowGraph = GetFlowGraph(compilation, graph);

            OperationTreeVerifier.Verify(expectedFlowGraph, actualFlowGraph);
        }