提交 767427bb 编写于 作者: B Balaji Krishnan

Add scaffolding code to test MoveType

AbstractMoveTypeTestsBase implements shared helpers for testing MoveType
Refactoring.

CSharpMoveTypeTestsBase and BasicMoveTypeTestsBase contain language
specific test helpers.

Tests are organized as MoveTypeTests.<CodeAction>.cs/vb

where code actions are
Move type to new file
Rename type to match filename
Rename file to match typename etc.

The feature code is mostly language agnostic, so it suffices if we pick
one language and test it heavily and make sure simple things and
language specific parts work as expected, on the other language.
上级 2f3fa3bc
......@@ -156,6 +156,9 @@
<Compile Include="CodeActions\IntroduceVariable\IntroduceVariableTests.cs" />
<Compile Include="CodeActions\InvertIf\InvertIfTests.cs" />
<Compile Include="CodeActions\LambdaSimplifier\LambdaSimplifierTests.cs" />
<Compile Include="CodeActions\MoveType\CSharpMoveTypeTestsBase.cs" />
<Compile Include="CodeActions\MoveType\MoveTypeTests.MoveToNewFile.cs" />
<Compile Include="CodeActions\MoveType\MoveTypeTests.RenameFile.cs" />
<Compile Include="CodeActions\ReplacePropertyWithMethods\ReplacePropertyWithMethodsTests.cs" />
<Compile Include="Completion\CompletionServiceTests.cs" />
<Compile Include="Diagnostics\AddUsing\AddUsingTests_NuGet.cs" />
......
// 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.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.MoveType;
using Microsoft.CodeAnalysis.Editor.UnitTests.MoveType;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions.MoveType
{
public abstract class CSharpMoveTypeTestsBase : AbstractMoveTypeTestsBase
{
protected override ParseOptions GetScriptOptions()
{
return Options.Script;
}
protected override Task<TestWorkspace> CreateWorkspaceFromFileAsync(string definition, ParseOptions parseOptions, CompilationOptions compilationOptions)
{
return TestWorkspace.CreateCSharpAsync(definition, (CSharpParseOptions)parseOptions, (CSharpCompilationOptions)compilationOptions);
}
protected override string GetLanguage()
{
return LanguageNames.CSharp;
}
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace)
{
return new MoveTypeCodeRefactoringProvider();
}
}
}
// 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.Threading.Tasks;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions.MoveType
{
public partial class MoveTypeTests : CSharpMoveTypeTestsBase
{
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task ClassesWithNoContainerNamespace()
{
var code =
@"[||]class Class1 { }
class Class2 { }";
var codeAfterMove = @"class Class2 { }";
var expectedDocumentName = "Class1.cs";
var destinationDocumentText = @"class Class1 { }";
await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task ClassesWithContainerNamespace()
{
var code =
@"namespace N1
{
[||]class Class1 { }
class Class2 { }
}";
var codeAfterMove =
@"namespace N1
{
class Class2 { }
}";
var expectedDocumentName = "Class1.cs";
var destinationDocumentText =
@"namespace N1
{
class Class1 { }
}";
await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText);
}
}
}
\ No newline at end of file
// 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.Threading.Tasks;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeActions.MoveType
{
public partial class MoveTypeTests : CSharpMoveTypeTestsBase
{
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)]
public async Task SingleClassInFileWithNoContainerNamespace()
{
var code =
@"[||]class Class1";
var expectedDocumentName = "Class1.cs";
await TestRenameFileToMatchTypeAsync(code, expectedDocumentName);
}
}
}
......@@ -218,6 +218,7 @@
<Compile Include="Extensions\SourceTextContainerExtensionsTests.cs" />
<Compile Include="Extensions\WorkspaceExtensions.cs" />
<Compile Include="GoToAdjacentMember\AbstractGoToAdjacentMemberTests.cs" />
<Compile Include="MoveType\AbstractMoveTypeTestsBase.cs" />
<Compile Include="Outlining\AbstractSyntaxNodeOutlinerTests.cs" />
<Compile Include="Outlining\AbstractSyntaxOutlinerTests.cs" />
<Compile Include="Outlining\AbstractSyntaxTriviaOutlinerTests.cs" />
......
// 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;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.UnitTests;
using Roslyn.Test.Utilities;
using Xunit;
namespace Microsoft.CodeAnalysis.Editor.UnitTests.MoveType
{
public abstract class AbstractMoveTypeTestsBase : AbstractCodeActionTest
{
private const string SpanMarker = "[||]";
private string StripSpanMarkers(string text)
{
var index = text.IndexOf(SpanMarker);
return text.Remove(index, SpanMarker.Length);
}
protected async Task TestRenameFileToMatchTypeAsync(
string originalCode,
string expectedDocumentName)
{
// TODO: Implement this.
await Task.Delay(1);
}
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;
var refactoring = await GetCodeRefactoringAsync(workspace);
// 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);
}
}
}
}
......@@ -68,6 +68,7 @@ public static class Features
public const string CodeActionsMakeMethodSynchronous = "CodeActions.MakeMethodSynchronous";
public const string CodeActionsMoveDeclarationNearReference = "CodeActions.MoveDeclarationNearReference";
public const string CodeActionsMoveToTopOfFile = "CodeActions.MoveToTopOfFile";
public const string CodeActionsMoveType = "CodeActions.MoveType";
public const string CodeActionsPopulateSwitch = "CodeActions.PopulateSwitch";
public const string CodeActionsQualifyMemberAccess = "CodeActions.QualifyMemberAccess";
public const string CodeActionsReplaceMethodWithProperty = "CodeActions.ReplaceMethodWithProperty";
......
......@@ -136,6 +136,9 @@
<Compile Include="CodeActions\InlineTemporary\InlineTemporaryTests.vb" />
<Compile Include="CodeActions\IntroduceVariable\IntroduceVariableTests.vb" />
<Compile Include="CodeActions\InvertIf\InvertIfTests.vb" />
<Compile Include="CodeActions\MoveType\BasicMoveTypeTestsBase.vb" />
<Compile Include="CodeActions\MoveType\MoveTypeTests.MoveToNewFile.vb" />
<Compile Include="CodeActions\MoveType\MoveTypeTests.RenameFile.vb" />
<Compile Include="CodeActions\Preview\PreviewTests.vb" />
<Compile Include="CodeActions\ReplacePropertyWithMethods\ReplacePropertyWithMethodsTests.vb" />
<Compile Include="Completion\CompletionServiceTests.vb" />
......
' 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 System.Xml.Linq
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.Editor.UnitTests.MoveType
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.MoveType
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings.MoveType
Public Class BasicMoveTypeTestsBase
Inherits AbstractMoveTypeTestsBase
Protected Overrides Function CreateCodeRefactoringProvider(workspace As Workspace) As CodeRefactoringProvider
Return New MoveTypeCodeRefactoringProvider()
End Function
Protected Overrides Function CreateWorkspaceFromFileAsync(
definition As String,
ParseOptions As ParseOptions,
CompilationOptions As CompilationOptions
) As Task(Of TestWorkspace)
Return TestWorkspace.CreateVisualBasicAsync(
definition,
ParseOptions,
If(CompilationOptions, New VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)))
End Function
Protected Overrides Function GetLanguage() As String
Return LanguageNames.VisualBasic
End Function
Protected Overrides Function GetScriptOptions() As ParseOptions
Return TestOptions.Script
End Function
Protected Overloads Function TestRenameFileToMatchTypeAsync(originalCode As XElement, expectedDocumentName As String) As Task
Return MyBase.TestRenameFileToMatchTypeAsync(originalCode.ConvertTestSourceTag(), expectedDocumentName)
End Function
Protected Overloads Function TestMoveTypeToNewFileAsync(
originalCode As XElement,
expectedSourceTextAfterRefactoring As XElement,
expectedDocumentName As String,
destinationDocumentText As XElement,
Optional destinationDocumentContainers As IList(Of String) = Nothing,
Optional expectedCodeAction As Boolean = True,
Optional index As Integer = 0,
Optional compareTokens As Boolean = True
) As Task
Dim originalCodeText = originalCode.ConvertTestSourceTag()
Dim expectedSourceText = expectedSourceTextAfterRefactoring.ConvertTestSourceTag()
Dim expectedDestinationText = destinationDocumentText.ConvertTestSourceTag()
Return MyBase.TestMoveTypeToNewFileAsync(
originalCodeText,
expectedSourceText,
expectedDocumentName,
expectedDestinationText,
destinationDocumentContainers,
expectedCodeAction,
index,
compareTokens)
End Function
End Class
End Namespace
' 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.Editor.VisualBasic.UnitTests.CodeRefactorings.MoveType
Partial Public Class MoveTypeTests
Inherits BasicMoveTypeTestsBase
<WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)>
Public Async Function ClassesWithNoContainerNamespace() As Task
Dim code =
<File>
[||]Class Class1
End Class
Class Class2
End Class
</File>
Dim codeAfterMove =
<File>
Class Class2
End Class
</File>
Dim expectedDocumentName = "Class1.vb"
Dim destinationDocumentText =
<File>
Class Class1
End Class
</File>
Await TestMoveTypeToNewFileAsync(code, codeAfterMove, expectedDocumentName, destinationDocumentText)
End Function
End Class
End Namespace
' 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.Editor.VisualBasic.UnitTests.CodeRefactorings.MoveType
Partial Public Class MoveTypeTests
<WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsMoveType)>
Public Async Function SingleClassInFileWithNoContainerNamespace() As Task
Dim code =
<File>
[||]Class Class1
End Class
</File>
Dim expectedDocumentName = "Class1.vb"
Await TestRenameFileToMatchTypeAsync(code, expectedDocumentName)
End Function
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册