Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port extract method tests #18583

Merged
merged 1 commit into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
using Microsoft.VisualStudio.IntegrationTest.Utilities.Input;
using Roslyn.Test.Utilities;
using Xunit;

namespace Roslyn.VisualStudio.IntegrationTests.CSharp
{
[Collection(nameof(SharedIntegrationHostFixture))]
public class CSharpExtractMethod : AbstractEditorTest
{
private const string TestSource = @"
using System;
public class Program
{
public int Method()
{
Console.WriteLine(""Hello World"");
int a;
int b;
a = 5;
b = 10;
int result = a * b;
return result;
}
}";

protected override string LanguageName => LanguageNames.CSharp;

public CSharpExtractMethod(VisualStudioInstanceFactory instanceFactory)
: base(instanceFactory, nameof(CSharpExtractMethod))
{
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void SimpleExtractMethod()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("Console", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("World", charsOffset: 4, extendSelection: true);
VisualStudio.ExecuteCommand(WellKnownCommandNames.Refactor_ExtractMethod);

var expectedMarkup = @"
using System;
public class Program
{
public int Method()
{
[|NewMethod|]();
int a;
int b;
a = 5;
b = 10;
int result = a * b;
return result;
}

private static void [|NewMethod|]() => Console.WriteLine(""Hello World"");
}";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
VisualStudio.Editor.Verify.TextContains(expectedText);
VisualStudio.Workspace.WaitForAsyncOperations(FeatureAttribute.Rename);
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));

VisualStudio.Editor.SendKeys("SayHello", VirtualKey.Enter);
VisualStudio.Editor.Verify.TextContains(@"private static void SayHello() => Console.WriteLine(""Hello World"")");
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void ExtractViaCodeAction()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("a = 5", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("a * b", charsOffset: 1, extendSelection: true);
VisualStudio.Editor.Verify.CodeAction("Extract Method", applyFix: true, blockUntilComplete: true);

var expectedMarkup = @"
using System;
public class Program
{
public int Method()
{
Console.WriteLine(""Hello World"");
int a;
int b;
int result;
[|NewMethod|](out a, out b, out result);
return result;
}

private static void [|NewMethod|](out int a, out int b, out int result)
{
a = 5;
b = 10;
result = a * b;
}
}";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void ExtractViaCodeActionWithMoveLocal()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("a = 5", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("a * b", charsOffset: 1, extendSelection: true);
try
{
VisualStudio.Workspace.SetFeatureOption("ExtractMethodOptions", "AllowMovingDeclaration", LanguageNames.CSharp, "true");
VisualStudio.Editor.Verify.CodeAction("Extract Method + Local", applyFix: true, blockUntilComplete: true);

var expectedMarkup = @"
using System;
public class Program
{
public int Method()
{
Console.WriteLine(""Hello World"");
var result = [|NewMethod|]();
return result;
}

private static int [|NewMethod|]()
{
int a, b;
a = 5;
b = 10;
int result = a * b;
return result;
}
}";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));
}
finally
{
VisualStudio.Workspace.SetFeatureOption("ExtractMethodOptions", "AllowMovingDeclaration", LanguageNames.CSharp, "false");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// 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.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.TestHooks;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.IntegrationTest.Utilities;
using Microsoft.VisualStudio.IntegrationTest.Utilities.Input;
using Roslyn.Test.Utilities;
using Xunit;

namespace Roslyn.VisualStudio.IntegrationTests.Basic
{
[Collection(nameof(SharedIntegrationHostFixture))]
public class BasicExtractMethod : AbstractEditorTest
{
private const string TestSource = @"
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Program
Sub Main(args As String())
Console.WriteLine(""Hello VB!"")
End Sub

Function F() As Integer
Dim a As Integer
Dim b As Integer
a = 5
b = 5
Dim result = a * b
Return result
End Function
End Module";

protected override string LanguageName => LanguageNames.VisualBasic;

public BasicExtractMethod(VisualStudioInstanceFactory instanceFactory)
: base(instanceFactory, nameof(BasicExtractMethod))
{
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void SimpleExtractMethod()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("Console", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("Hello VB!", charsOffset: 3, extendSelection: true);
VisualStudio.ExecuteCommand(WellKnownCommandNames.Refactor_ExtractMethod);

var expectedMarkup = @"
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Program
Sub Main(args As String())
[|NewMethod|]()
End Sub

Private Sub [|NewMethod|]()
Console.WriteLine(""Hello VB!"")
End Sub

Function F() As Integer
Dim a As Integer
Dim b As Integer
a = 5
b = 5
Dim result = a * b
Return result
End Function
End Module";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
VisualStudio.Editor.Verify.TextContains(expectedText);
VisualStudio.Workspace.WaitForAsyncOperations(FeatureAttribute.Rename);
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));

VisualStudio.Editor.SendKeys("SayHello", VirtualKey.Enter);
VisualStudio.Editor.Verify.TextContains(@" Private Sub SayHello()
Console.WriteLine(""Hello VB!"")
End Sub");
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void ExtractViaCodeAction()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("a = 5", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("a * b", charsOffset: 1, extendSelection: true);
VisualStudio.Editor.Verify.CodeAction("Extract Method", applyFix: true, blockUntilComplete: true);

var expectedMarkup = @"
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Program
Sub Main(args As String())
Console.WriteLine(""Hello VB!"")
End Sub

Function F() As Integer
Dim a As Integer
Dim b As Integer
Dim result As Integer = Nothing
[|NewMethod|](a, b, result)
Return result
End Function

Private Sub [|NewMethod|](ByRef a As Integer, ByRef b As Integer, ByRef result As Integer)
a = 5
b = 5
result = a * b
End Sub
End Module";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));
}

[Fact, Trait(Traits.Feature, Traits.Features.ExtractMethod)]
public void ExtractViaCodeActionWithMoveLocal()
{
VisualStudio.Editor.SetText(TestSource);
VisualStudio.Editor.PlaceCaret("a = 5", charsOffset: -1);
VisualStudio.Editor.PlaceCaret("a * b", charsOffset: 1, extendSelection: true);
try
{
VisualStudio.Workspace.SetFeatureOption("ExtractMethodOptions", "AllowMovingDeclaration", LanguageNames.VisualBasic, "true");
VisualStudio.Editor.Verify.CodeAction("Extract Method + Local", applyFix: true, blockUntilComplete: true);

var expectedMarkup = @"
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module Program
Sub Main(args As String())
Console.WriteLine(""Hello VB!"")
End Sub

Function F() As Integer
Dim result As Integer = [|NewMethod|]()
Return result
End Function

Private Function [|NewMethod|]() As Integer
Dim a, b As Integer
a = 5
b = 5
Dim result = a * b
Return result
End Function
End Module";

MarkupTestFile.GetSpans(expectedMarkup, out var expectedText, out ImmutableArray<TextSpan> spans);
Assert.Equal(expectedText, VisualStudio.Editor.GetText());
AssertEx.SetEqual(spans, VisualStudio.Editor.GetTagSpans(VisualStudio.InlineRenameDialog.ValidRenameTag));
}
finally
{
VisualStudio.Workspace.SetFeatureOption("ExtractMethodOptions", "AllowMovingDeclaration", LanguageNames.VisualBasic, "false");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="CSharp\CSharpNavigationBar.cs" />
<Compile Include="CSharp\CSharpKeywordHighlighting.cs" />
<Compile Include="CSharp\CSharpOutlining.cs" />
<Compile Include="CSharp\CSharpExtractMethod.cs" />
<Compile Include="CSharp\CSharpReplClassification.cs" />
<Compile Include="CSharp\CSharpReplIdeFeatures.cs" />
<Compile Include="CSharp\CSharpReplIntellisense.cs" />
Expand Down Expand Up @@ -75,6 +76,7 @@
<Compile Include="VisualBasic\BasicErrorListNetCore.cs" />
<Compile Include="VisualBasic\BasicOrganizing.cs" />
<Compile Include="VisualBasic\BasicOutlining.cs" />
<Compile Include="VisualBasic\BasicExtractMethod.cs" />
<Compile Include="Workspace\WorkspaceBase.cs" />
<Compile Include="Workspace\WorkspacesDesktop.cs" />
<Compile Include="Workspace\WorkspacesNetCore.cs" />
Expand Down