protected async Task IterateAllAsync(string code)
        {
            using (var workspace = await TestWorkspace.CreateCSharpAsync(code, CodeAnalysis.CSharp.Test.Utilities.TestOptions.Regular))
            {
                var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
                Assert.NotNull(document);

                var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
                var root = await document.GetSyntaxRootAsync();
                var iterator = root.DescendantNodesAndSelf().Cast<SyntaxNode>();

                var originalOptions = await document.GetOptionsAsync();
                var options = originalOptions.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);

                foreach (var node in iterator)
                {
                    var validator = new CSharpSelectionValidator(semanticDocument, node.Span, options);
                    var result = await validator.GetValidSelectionAsync(CancellationToken.None);

                    // check the obvious case
                    if (!(node is ExpressionSyntax) && !node.UnderValidContext())
                    {
                        Assert.True(result.Status.FailedWithNoBestEffortSuggestion());
                    }
                }
            }
        }
        protected static async Task<SyntaxNode> ExtractMethodAsync(
            TestWorkspace workspace,
            TestHostDocument testDocument,
            bool succeed = true,
            bool allowMovingDeclaration = true,
            bool dontPutOutOrRefOnStruct = true)
        {
            var document = workspace.CurrentSolution.GetDocument(testDocument.Id);
            Assert.NotNull(document);

            var originalOptions = await document.GetOptionsAsync();
            var options = originalOptions.WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, allowMovingDeclaration)
                                         .WithChangedOption(ExtractMethodOptions.DontPutOutOrRefOnStruct, document.Project.Language, dontPutOutOrRefOnStruct);

            var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
            var validator = new CSharpSelectionValidator(semanticDocument, testDocument.SelectedSpans.Single(), options);

            var selectedCode = await validator.GetValidSelectionAsync(CancellationToken.None);
            if (!succeed && selectedCode.Status.FailedWithNoBestEffortSuggestion())
            {
                return null;
            }

            Assert.True(selectedCode.ContainsValidContext);

            // extract method
            var extractor = new CSharpMethodExtractor((CSharpSelectionResult)selectedCode);
            var result = await extractor.ExtractMethodAsync(CancellationToken.None);
            Assert.NotNull(result);
            Assert.Equal(succeed, result.Succeeded || result.SucceededWithSuggestion);

            return await result.Document.GetSyntaxRootAsync();
        }
        protected async Task TestSelectionAsync(string codeWithMarker, bool expectedFail = false, CSharpParseOptions parseOptions = null)
        {
            using (var workspace = await TestWorkspace.CreateCSharpAsync(codeWithMarker, parseOptions: parseOptions))
            {
                var testDocument = workspace.Documents.Single();
                var namedSpans = testDocument.AnnotatedSpans;

                var document = workspace.CurrentSolution.GetDocument(testDocument.Id);
                Assert.NotNull(document);

                var options = (await document.GetOptionsAsync(CancellationToken.None))
                    .WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);

                var semanticDocument = await SemanticDocument.CreateAsync(document, CancellationToken.None);
                var validator = new CSharpSelectionValidator(semanticDocument, namedSpans["b"].Single(), options);
                var result = await validator.GetValidSelectionAsync(CancellationToken.None);

                Assert.True(expectedFail ? result.Status.Failed() : result.Status.Succeeded());

                if ((result.Status.Succeeded() || result.Status.Flag.HasBestEffort()) && result.Status.Flag.HasSuggestion())
                {
                    Assert.Equal(namedSpans["r"].Single(), result.FinalSpan);
                }
            }
        }
Beispiel #4
0
        protected void IterateAll(string code)
        {
            using (var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromLines(new string[] { code }, CodeAnalysis.CSharp.Test.Utilities.TestOptions.Regular))
            {
                var document = workspace.CurrentSolution.GetDocument(workspace.Documents.First().Id);
                Assert.NotNull(document);

                var semanticDocument = SemanticDocument.CreateAsync(document, CancellationToken.None).Result;
                var tree = document.GetSyntaxTreeAsync().Result;
                var iterator = tree.GetRoot().DescendantNodesAndSelf().Cast<SyntaxNode>();

                var options = document.Project.Solution.Workspace.Options
                                      .WithChangedOption(ExtractMethodOptions.AllowMovingDeclaration, document.Project.Language, true);

                foreach (var node in iterator)
                {
                    try
                    {
                        var validator = new CSharpSelectionValidator(semanticDocument, node.Span, options);
                        var result = validator.GetValidSelectionAsync(CancellationToken.None).Result;

                        // check the obvious case
                        if (!(node is ExpressionSyntax) && !node.UnderValidContext())
                        {
                            Assert.True(result.Status.FailedWithNoBestEffortSuggestion());
                        }
                    }
                    catch (ArgumentException)
                    {
                        // catch and ignore unknown issue. currently control flow analysis engine doesn't support field initializer.
                    }
                }
            }
        }