AbstractMoveTypeTest.cs 8.1 KB
Newer Older
1 2 3 4 5
// 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 System.Collections.Generic;
using System.Linq;
B
Balaji Krishnan 已提交
6
using System.Threading;
7
using System.Threading.Tasks;
8 9
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CodeRefactorings.MoveType;
10
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
B
Balaji Krishnan 已提交
11
using Microsoft.CodeAnalysis.Text;
12 13 14 15 16 17
using Microsoft.CodeAnalysis.UnitTests;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.MoveType
{
18
    public abstract class AbstractMoveTypeTest : AbstractCodeActionTest
19
    {
20 21
        private string RenameFileCodeActionTitle = FeaturesResources.Rename_file_to_0;
        private string RenameTypeCodeActionTitle = FeaturesResources.Rename_type_to_0;
22

23 24 25 26 27
        protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace)
        {
            return new MoveTypeCodeRefactoringProvider();
        }

B
Balaji Krishnan 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
        protected async Task TestRenameTypeToMatchFileAsync(
           string originalCode,
           string expectedCode = null,
           bool expectedCodeAction = true,
           bool compareTokens = true)
        {
            using (var workspace = await CreateWorkspaceFromFileAsync(originalCode, parseOptions: null, compilationOptions: null))
            {
                if (expectedCodeAction)
                {
                    Assert.True(expectedCode != null, $"{nameof(expectedCode)} should be present if {nameof(expectedCodeAction)} is true.");

                    var documentId = workspace.Documents[0].Id;
                    var documentName = workspace.Documents[0].Name;

B
Balaji Krishnan 已提交
43 44 45 46 47 48
                    string expectedText;
                    TextSpan span;
                    MarkupTestFile.GetSpan(expectedCode, out expectedText, out span);

                    var codeActionTitle = string.Format(RenameTypeCodeActionTitle, expectedText.Substring(span.Start, span.Length));

B
Balaji Krishnan 已提交
49
                    var oldSolutionAndNewSolution = await TestOperationAsync(
B
Balaji Krishnan 已提交
50
                        workspace, expectedText, codeActionTitle, compareTokens);
B
Balaji Krishnan 已提交
51 52 53 54 55 56 57 58 59 60 61

                    // the original source document does not exist in the new solution.
                    var newSolution = oldSolutionAndNewSolution.Item2;

                    var document = newSolution.GetDocument(documentId);
                    Assert.NotNull(document);
                    Assert.Equal(documentName, document.Name);
                }
                else
                {
                    var actions = await GetCodeActionsAsync(workspace, fixAllActionEquivalenceKey: null);
B
Balaji Krishnan 已提交
62 63 64 65 66 67

                    if (actions != null)
                    {
                        var renameFileAction = actions.Any(action => action.Title.StartsWith(RenameTypeCodeActionTitle));
                        Assert.False(renameFileAction, "Rename Type to match file name code action was not expected, but shows up.");
                    }
B
Balaji Krishnan 已提交
68 69 70 71
                }
            }
        }

72 73
        protected async Task TestRenameFileToMatchTypeAsync(
            string originalCode,
B
Balaji Krishnan 已提交
74 75 76 77 78 79 80 81 82 83 84
            string expectedDocumentName = null,
            bool expectedCodeAction = true,
            bool compareTokens = true)
        {
            using (var workspace = await CreateWorkspaceFromFileAsync(originalCode, parseOptions: null, compilationOptions: null))
            {
                if (expectedCodeAction)
                {
                    Assert.True(expectedDocumentName != null, $"{nameof(expectedDocumentName)} should be present if {nameof(expectedCodeAction)} is true.");

                    var oldDocumentId = workspace.Documents[0].Id;
B
Balaji Krishnan 已提交
85 86 87 88 89 90

                    string expectedText;
                    IList<TextSpan> spans;
                    MarkupTestFile.GetSpans(originalCode, out expectedText, out spans);

                    var codeActionTitle = string.Format(RenameFileCodeActionTitle, expectedDocumentName);
B
Balaji Krishnan 已提交
91 92 93

                    // a new document with the same text as old document is added.
                    var oldSolutionAndNewSolution = await TestOperationAsync(
B
Balaji Krishnan 已提交
94
                        workspace, expectedText, codeActionTitle, compareTokens);
B
Balaji Krishnan 已提交
95 96 97 98 99 100 101 102

                    // the original source document does not exist in the new solution.
                    var newSolution = oldSolutionAndNewSolution.Item2;
                    Assert.Null(newSolution.GetDocument(oldDocumentId));
                }
                else
                {
                    var actions = await GetCodeActionsAsync(workspace, fixAllActionEquivalenceKey: null);
B
Balaji Krishnan 已提交
103 104 105 106 107 108

                    if (actions != null)
                    {
                        var renameFileAction = actions.Any(action => action.Title.StartsWith(RenameFileCodeActionTitle));
                        Assert.False(renameFileAction, "Rename File to match type code action was not expected, but shows up.");
                    }
B
Balaji Krishnan 已提交
109 110 111 112 113 114 115 116 117
                }
            }
        }

        private async Task<Tuple<Solution, Solution>> TestOperationAsync(
            Workspaces.TestWorkspace workspace,
            string expectedCode,
            string operation,
            bool compareTokens)
118
        {
B
Balaji Krishnan 已提交
119 120 121 122 123 124 125 126 127 128 129 130
            var actions = await GetCodeActionsAsync(workspace, fixAllActionEquivalenceKey: null);
            var action = actions.Single(a => a.Title.StartsWith(operation));
            var operations = await action.GetOperationsAsync(CancellationToken.None);

            return await TestOperationsAsync(workspace,
                expectedText: expectedCode,
                operations: operations,
                conflictSpans: null,
                renameSpans: null,
                warningSpans: null,
                compareTokens: compareTokens,
                expectedChangedDocumentId: null);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        }

        protected async Task TestMoveTypeToNewFileAsync(
            string originalCode,
            string expectedSourceTextAfterRefactoring,
            string expectedDocumentName,
            string destinationDocumentText,
            IList<string> destinationDocumentContainers = null,
            bool expectedCodeAction = true,
            int index = 0,
            bool compareTokens = true)
        {
            if (expectedCodeAction)
            {
                using (var workspace = await CreateWorkspaceFromFileAsync(originalCode, parseOptions: null, compilationOptions: null))
                {
                    // replace with default values on null.
                    if (destinationDocumentContainers == null)
                    {
                        destinationDocumentContainers = Array.Empty<string>();
                    }

                    var sourceDocumentId = workspace.Documents[0].Id;

                    // Verify the newly added document and its text
                    var oldSolutionAndNewSolution = await TestAddDocumentAsync(workspace,
                        destinationDocumentText, index, expectedDocumentName, destinationDocumentContainers, compareTokens: compareTokens);

                    // Verify source document's text after moving type.
                    var oldSolution = oldSolutionAndNewSolution.Item1;
                    var newSolution = oldSolutionAndNewSolution.Item2;
                    var changedDocumentIds = SolutionUtilities.GetChangedDocuments(oldSolution, newSolution);
                    Assert.True(changedDocumentIds.Contains(sourceDocumentId), "source document was not changed.");

                    var modifiedSourceDocument = newSolution.GetDocument(sourceDocumentId);

                    if (compareTokens)
                    {
                        TokenUtilities.AssertTokensEqual(
                            expectedSourceTextAfterRefactoring, (await modifiedSourceDocument.GetTextAsync()).ToString(), GetLanguage());
                    }
                    else
                    {
                        Assert.Equal(expectedSourceTextAfterRefactoring, (await modifiedSourceDocument.GetTextAsync()).ToString());
                    }
                }
            }
            else
            {
                await TestMissingAsync(originalCode, parseOptions: null);
            }
        }
    }
}