AbstractMoveTypeTest.cs 7.8 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 11 12 13 14 15 16
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.UnitTests;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.MoveType
{
17
    public abstract class AbstractMoveTypeTest : AbstractCodeActionTest
18 19
    {
        private const string SpanMarker = "[||]";
B
Balaji Krishnan 已提交
20 21
        private const string RenameFileCodeActionTitle = "Rename File";
        private const string RenameTypeCodeActionTitle = "Rename Type";
22 23 24 25 26 27 28

        private string StripSpanMarkers(string text)
        {
            var index = text.IndexOf(SpanMarker);
            return text.Remove(index, SpanMarker.Length);
        }

29 30 31 32 33
        protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace)
        {
            return new MoveTypeCodeRefactoringProvider();
        }

B
Balaji Krishnan 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
        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;

                    var oldSolutionAndNewSolution = await TestOperationAsync(
                        workspace, expectedCode, RenameTypeCodeActionTitle, compareTokens);

                    // 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 85 86 87 88 89 90 91 92 93 94 95 96 97
            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;
                    var expectedText = StripSpanMarkers(originalCode);

                    // a new document with the same text as old document is added.
                    var oldSolutionAndNewSolution = await TestOperationAsync(
                        workspace, expectedText, RenameFileCodeActionTitle, compareTokens);

                    // 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 已提交
98 99 100 101 102 103

                    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 已提交
104 105 106 107 108 109 110 111 112
                }
            }
        }

        private async Task<Tuple<Solution, Solution>> TestOperationAsync(
            Workspaces.TestWorkspace workspace,
            string expectedCode,
            string operation,
            bool compareTokens)
113
        {
B
Balaji Krishnan 已提交
114 115 116 117 118 119 120 121 122 123 124 125
            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);
126 127 128 129 130 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
        }

        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);
            }
        }
    }
}