Skip to content

Commit

Permalink
Offer add parameter to constructor refactoring for all applicable con…
Browse files Browse the repository at this point in the history
…structors (#34041)

* Fixes 33603 and 33623

* Respond to feedback

* Fix spelling/random characters

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* Use IsEmpty property

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* remove unneeded null check

* Configure await false

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* refactor null check

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* Respond to feedback and refactor

* Wrap long lines

* Update test comments

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* Change .AsImmutableOrNull to .ToImmutableAndFree

Co-Authored-By: chborl <chborl@users.noreply.github.com>

* Fix test doc comments

* Apply suggestions from code review

Update doc comment

Co-Authored-By: chborl <chborl@users.noreply.github.com>
  • Loading branch information
chborl authored Mar 26, 2019
1 parent 3617933 commit a515307
Show file tree
Hide file tree
Showing 22 changed files with 1,104 additions and 118 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public struct TestParameters
internal readonly int index;
internal readonly CodeActionPriority? priority;
internal readonly bool retainNonFixableDiagnostics;
internal readonly string title;

internal TestParameters(
ParseOptions parseOptions = null,
Expand All @@ -45,7 +46,8 @@ internal TestParameters(
object fixProviderData = null,
int index = 0,
CodeActionPriority? priority = null,
bool retainNonFixableDiagnostics = false)
bool retainNonFixableDiagnostics = false,
string title = null)
{
this.parseOptions = parseOptions;
this.compilationOptions = compilationOptions;
Expand All @@ -54,16 +56,17 @@ internal TestParameters(
this.index = index;
this.priority = priority;
this.retainNonFixableDiagnostics = retainNonFixableDiagnostics;
this.title = title;
}

public TestParameters WithParseOptions(ParseOptions parseOptions)
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority);
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority, title: title);

public TestParameters WithFixProviderData(object fixProviderData)
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority);
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority, title: title);

public TestParameters WithIndex(int index)
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority);
=> new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, priority, title: title);
}

protected abstract string GetLanguage();
Expand Down Expand Up @@ -323,11 +326,12 @@ internal Task TestInRegularAndScriptAsync(
CompilationOptions compilationOptions = null,
IDictionary<OptionKey, object> options = null,
object fixProviderData = null,
ParseOptions parseOptions = null)
ParseOptions parseOptions = null,
string title = null)
{
return TestInRegularAndScript1Async(
initialMarkup, expectedMarkup, index, priority,
new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index));
new TestParameters(parseOptions, compilationOptions, options, fixProviderData, index, title: title));
}

internal async Task TestInRegularAndScript1Async(
Expand Down Expand Up @@ -524,6 +528,11 @@ internal static Task<ImmutableArray<CodeActionOperation>> VerifyActionAndGetOper
Assert.Equal(parameters.priority.Value, action.Priority);
}

if (parameters.title != null)
{
Assert.Equal(parameters.title, action.Title);
}

return action.GetOperationsAsync(CancellationToken.None);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
' 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.Collections.Immutable
Imports Microsoft.CodeAnalysis.AddConstructorParametersFromMembers
Imports Microsoft.CodeAnalysis.CodeActions
Imports Microsoft.CodeAnalysis.CodeRefactorings
Imports Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings

Expand All @@ -12,7 +14,12 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.AddConstructorPara
Return New AddConstructorParametersFromMembersCodeRefactoringProvider()
End Function

Protected Overrides Function MassageActions(actions As ImmutableArray(Of CodeAction)) As ImmutableArray(Of CodeAction)
Return FlattenActions(actions)
End Function

<WorkItem(530592, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530592")>
<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestAdd1() As Task
Await TestInRegularAndScriptAsync(
Expand All @@ -30,10 +37,11 @@ End Class",
Me.i = i
Me.s = s
End Sub
End Class")
End Class", title:=String.Format(FeaturesResources.Add_parameters_to_0, "Program(Integer)"))
End Function

<WorkItem(530592, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530592")>
<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestAddOptional1() As Task
Await TestInRegularAndScriptAsync(
Expand All @@ -51,13 +59,14 @@ End Class",
Me.i = i
Me.s = s
End Sub
End Class",
index:=1)
End Class", index:=1, title:=String.Format(FeaturesResources.Add_optional_parameters_to_0, "Program(Integer)"))
End Function

<WorkItem(530592, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530592")>
<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestAddToConstructorWithMostMatchingParameters1() As Task
' behavior change with 33603, now all constructors offered
Await TestInRegularAndScriptAsync(
"Class Program
[|Private i As Integer
Expand All @@ -83,12 +92,14 @@ End Class",
Me.s = s
Me.b = b
End Sub
End Class")
End Class", index:=1, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, String)"))
End Function

<WorkItem(530592, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530592")>
<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestAddOptionalToConstructorWithMostMatchingParameters1() As Task
' behavior change with 33603, now all constructors offered
Await TestInRegularAndScriptAsync(
"Class Program
[|Private i As Integer
Expand All @@ -114,8 +125,7 @@ End Class",
Me.s = s
Me.b = b
End Sub
End Class",
index:=1)
End Class", index:=3, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, String)"))
End Function

<WorkItem(28775, "https://github.com/dotnet/roslyn/issues/28775")>
Expand Down Expand Up @@ -304,5 +314,252 @@ End Class",
End Sub
End Class")
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_FirstOfThree() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer)
Me.i = i
End Sub

Public Sub New(i As Integer, j As Integer)
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer)
End Sub
End Class",
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer, l As Integer)
Me.i = i
Me.l = l
End Sub

Public Sub New(i As Integer, j As Integer)
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer)
End Sub
End Class", index:=0, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_SecondOfThree() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer)
Me.i = i
End Sub

Public Sub New(i As Integer, j As Integer)
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer)
End Sub
End Class",
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer)
Me.i = i
End Sub

Public Sub New(i As Integer, j As Integer, l As Integer)
Me.l = l
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer)
End Sub
End Class", index:=1, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, Integer)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_ThirdOfThree() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer)
Me.i = i
End Sub

Public Sub New(i As Integer, j As Integer)
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer)
End Sub
End Class",
"Class Program
Private [|l|] As Integer

Public Sub New(i As Integer)
Me.i = i
End Sub

Public Sub New(i As Integer, j As Integer)
End Sub

Public Sub New(i As Integer, j As Integer, k As Integer, l As Integer)
Me.l = l
End Sub
End Class", index:=2, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, Integer, Integer)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_OneMustBeOptional() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|l|] As Integer

' index 0 as required
' index 2 as optional
Public Sub New(i As Integer)
Me.i = i
End Sub

' index 3 as optional
Public Sub New(Optional j As Double = Nothing)
End Sub

' index 1 as required
' index 4 as optional
Public Sub New(i As Integer, j As Double)
End Sub
End Class",
"Class Program
Private [|l|] As Integer

' index 0 as required
' index 2 as optional
Public Sub New(i As Integer)
Me.i = i
End Sub

' index 3 as optional
Public Sub New(Optional j As Double = Nothing)
End Sub

' index 1 as required
' index 4 as optional
Public Sub New(i As Integer, j As Double, l As Integer)
Me.l = l
End Sub
End Class", index:=1, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, Double)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_OneMustBeOptional2() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|l|] As Integer

' index 0, and 2 as optional
Public Sub New(i As Integer)
Me.i = i
End Sub

' index 3 as optional
Public Sub New(Optional j As Double = Nothing)
End Sub

' index 1, and 4 as optional
Public Sub New(i As Integer, j As Double)
End Sub
End Class",
"Class Program
Private [|l|] As Integer

' index 0, and 2 as optional
Public Sub New(i As Integer)
Me.i = i
End Sub

' index 3 as optional
Public Sub New(Optional j As Double = Nothing, Optional l As Integer = Nothing)
Me.l = l
End Sub

' index 1, and 4 as optional
Public Sub New(i As Integer, j As Double)
End Sub
End Class", index:=3, title:=String.Format(FeaturesResources.Add_to_0, "Program(Double)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_AllMustBeOptional1() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|p|] As Integer

Public Sub New(Optional i As Integer = Nothing)
Me.i = i
End Sub

Public Sub New(j As Double, Optional k As Double = Nothing)
End Sub

Public Sub New(l As Integer, m As Integer, Optional n As Integer = Nothing)
End Sub
End Class",
"Class Program
Private p As Integer

Public Sub New(Optional i As Integer = Nothing, Optional p As Integer = Nothing)
Me.i = i
Me.p = p
End Sub

Public Sub New(j As Double, Optional k As Double = Nothing)
End Sub

Public Sub New(l As Integer, m As Integer, Optional n As Integer = Nothing)
End Sub
End Class", index:=0, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer)"))
End Function

<WorkItem(33603, "https://github.com/dotnet/roslyn/issues/33603")>
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddConstructorParametersFromMembers)>
Public Async Function TestMultipleConstructors_AllMustBeOptional2() As Task
Await TestInRegularAndScriptAsync(
"Class Program
Private [|p|] As Integer

Public Sub New(Optional i As Integer = Nothing)
Me.i = i
End Sub

Public Sub New(j As Double, Optional k As Double = Nothing)
End Sub

Public Sub New(l As Integer, m As Integer, Optional n As Integer = Nothing)
End Sub
End Class",
"Class Program
Private p As Integer

Public Sub New(Optional i As Integer = Nothing)
Me.i = i
End Sub

Public Sub New(j As Double, Optional k As Double = Nothing)
End Sub

Public Sub New(l As Integer, m As Integer, Optional n As Integer = Nothing, Optional p As Integer = Nothing)
Me.p = p
End Sub
End Class", index:=2, title:=String.Format(FeaturesResources.Add_to_0, "Program(Integer, Integer, Integer)"))
End Function
End Class
End Namespace
End Namespace
Loading

0 comments on commit a515307

Please sign in to comment.