From 803dbecf77549b435dbe63d3669882900d47d11c Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 29 Jan 2025 13:37:11 -0800 Subject: [PATCH 1/3] All expression body compute refactorings on non-empty selections. --- .../UseExpressionBodyCodeRefactoringProvider.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Features/CSharp/Portable/UseExpressionBody/UseExpressionBodyCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/UseExpressionBody/UseExpressionBodyCodeRefactoringProvider.cs index 5edbf8a0f3448..c34dbb872552c 100644 --- a/src/Features/CSharp/Portable/UseExpressionBody/UseExpressionBodyCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/UseExpressionBody/UseExpressionBodyCodeRefactoringProvider.cs @@ -58,12 +58,10 @@ internal sealed class UseExpressionBodyCodeRefactoringProvider() : SyntaxEditorB public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context) { var (document, textSpan, cancellationToken) = context; - if (textSpan.Length > 0) - return; var position = textSpan.Start; var root = await document.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - var node = root.FindToken(position).Parent!; + var node = root.FindNode(textSpan); var containingLambda = node.FirstAncestorOrSelf(); if (containingLambda != null && From 9fbd3937d4633d3cbfbaf8acdf46d95fbea46eeb Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 29 Jan 2025 13:41:14 -0800 Subject: [PATCH 2/3] Add tests for the various UseExpressionBody refactoring tests that have non-empty selections --- ...ressionBodyForAccessorsRefactoringTests.cs | 47 ++++++++++++ ...sionBodyForConstructorsRefactoringTests.cs | 38 ++++++++++ ...yForConversionOperatorsRefactoringTests.cs | 38 ++++++++++ ...pressionBodyForIndexersRefactoringTests.cs | 44 +++++++++++ ...onBodyForLocalFunctionsRefactoringTests.cs | 47 ++++++++++++ ...xpressionBodyForMethodsRefactoringTests.cs | 73 +++++++++++++++++++ ...ressionBodyForOperatorsRefactoringTests.cs | 38 ++++++++++ ...essionBodyForPropertiesRefactoringTests.cs | 44 +++++++++++ 8 files changed, 369 insertions(+) diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs index 5011b0a17dae3..53d3e4ad42a1c 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs @@ -286,4 +286,51 @@ int Goo } }", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideExpressionBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + int Goo + { + get + { + return [|Bar()|]; + } + } + } + """, + """ + class C + { + int Goo + { + get => Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideExpressionBody() + { + await TestMissingAsync( + """ + class C + { + int Goo + { + get + { + return [|Bar(); + } + }|] + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs index b6d1855b4d4e7..2e6f9a923d75f 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs @@ -137,4 +137,42 @@ public C() } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideExpressionBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + public C() + { + [|Bar()|]; + } + } + """, + """ + class C + { + public C() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideExpressionBody() + { + await TestMissingAsync( + """ + class C + { + public C() + { + [|Bar(); + } + }|] + """, + parameters: new TestParameters(options: UseBlockBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs index 133ea945e407a..3a2b87baec824 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs @@ -137,4 +137,42 @@ public static implicit operator bool(C c1) } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + public static implicit operator bool(C c1) + { + [|Bar()|]; + } + } + """, + """ + class C + { + public static implicit operator bool(C c1) => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingAsync( + """ + class C + { + public static implicit operator bool(C c1) + { + [|Bar(); + } + }|] + """, + parameters: new TestParameters(options: UseBlockBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs index 4e49850fffdf7..32d87cf03a1c3 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs @@ -156,4 +156,48 @@ int this[int i] } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + int this[int i] + { + get + { + [|return Bar()|]; + } + } + } + """, + """ + class C + { + int this[int i] => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingAsync( + """ + class C + { + int this[int i] + { + get + { + [|return Bar(); + } + } + }|] + """, + parameters: new TestParameters(options: UseBlockBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs index eba9f49b0322c..6607e1a53be24 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs @@ -154,4 +154,51 @@ void Bar() } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + void Goo() + { + void Bar() + { + [|Test()|]; + } + } + } + """, + """ + class C + { + void Goo() + { + void Bar() => Test(); + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingAsync( + """ + class C + { + void Goo() + { + void Bar() + { + [|Test(); + } + }|] + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs index b1a4b95308780..a4d8f846211d0 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs @@ -237,4 +237,77 @@ void M() } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + void Goo() + { + [|Bar()|]; + } + } + """, + """ + class C + { + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingInRegularAndScriptAsync( + """ + class C + { + void Goo() + { + [|Bar(); + } + }|] + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestOfferedWithSelectionInsideExpressionBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + void Goo() => [|Bar()|]; + } + """, + """ + class C + { + void Goo() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideExpressionBody() + { + await TestMissingInRegularAndScriptAsync( + """ + class C + { + void Goo() => [|Bar(); + }|] + """, + parameters: new TestParameters(options: UseExpressionBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs index efd9fa80f8460..ff1249eaac430 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs @@ -137,4 +137,42 @@ await TestInRegularAndScript1Async( } }", parameters: new TestParameters(options: UseExpressionBody)); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + public static bool operator +(C c1, C c2) + { + [|Bar()|]; + } + } + """, + """ + class C + { + public static bool operator +(C c1, C c2) => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingAsync( + """ + class C + { + public static bool operator +(C c1, C c2) + { + [|Bar(); + } + }|] + """, + parameters: new TestParameters(options: UseBlockBody)); + } } diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs index cbd6e5394f8f4..8012ce3346234 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs @@ -308,4 +308,48 @@ int Goo }", parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6), options: UseExpressionBodyForAccessors_ExpressionBodyForProperties); } + + [Fact] + public async Task TestOfferedWithSelectionInsideBlockBody() + { + await TestInRegularAndScript1Async( + """ + class C + { + int Goo + { + get + { + [|return Bar()|]; + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + } + + [Fact] + public async Task TestNotOfferedWithSelectionOutsideBlockBody() + { + await TestMissingAsync( + """ + class C + { + int Goo + { + get + { + [|return Bar(); + } + } + }|] + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + } } From 6a95f981f4f83b84e5dc6387759582690cb2cb05 Mon Sep 17 00:00:00 2001 From: Todd Grunke Date: Wed, 29 Jan 2025 13:42:54 -0800 Subject: [PATCH 3/3] Convert other tests in modified files to raw strings --- ...ressionBodyForAccessorsRefactoringTests.cs | 322 ++++++++------- ...sionBodyForConstructorsRefactoringTests.cs | 153 ++++--- ...yForConversionOperatorsRefactoringTests.cs | 153 ++++--- ...pressionBodyForIndexersRefactoringTests.cs | 189 +++++---- ...onBodyForLocalFunctionsRefactoringTests.cs | 196 +++++---- ...xpressionBodyForMethodsRefactoringTests.cs | 309 +++++++++------ ...ressionBodyForOperatorsRefactoringTests.cs | 153 ++++--- ...essionBodyForPropertiesRefactoringTests.cs | 373 ++++++++++-------- 8 files changed, 1079 insertions(+), 769 deletions(-) diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs index 53d3e4ad42a1c..df68a60fc7121 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForAccessorsRefactoringTests.cs @@ -83,208 +83,264 @@ private OptionsCollection UseBlockBodyForAccessors_BlockBodyForProperties_Disabl public async Task TestUpdatePropertyIfPropertyWantsBlockAndAccessorWantsExpression() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody2() { await TestMissingAsync( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - return [||]Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo + { + get + { + return [||]Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody2() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - return [||]Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo + { + get + { + return [||]Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - return [||]Bar(); - } - } -}", -@"class C -{ - int Goo - { - get => Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo + { + get + { + return [||]Bar(); + } + } + } + """, + """ + class C + { + int Goo + { + get => Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferExpressionBodyForPropertyIfPropertyAndAccessorBothPreferExpressions() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - return [||]Bar(); - } - } -}", -@"class C -{ - int Goo => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo + { + get + { + return [||]Bar(); + } + } + } + """, + """ + class C + { + int Goo => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - int Goo { get => [||]Bar(); } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo { get => [||]Bar(); } -}", + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody2() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo { get => [||]Bar(); } -}", + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestOfferedForPropertyIfUserPrefersBlockPropertiesAndHasBlockProperty() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo { get => [||]Bar(); } -}", + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestOfferForPropertyIfPropertyPrefersBlockButCouldBecomeExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo { get => [||]Bar(); } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20350")] public async Task TestAccessorListFormatting() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo { get => [||]Bar(); } -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo { get => [||]Bar(); } + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs index 2e6f9a923d75f..8fa5b0c2b58a0 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConstructorsRefactoringTests.cs @@ -38,104 +38,133 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - public C() - { - [||]Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public C() + { + [||]Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public C() - { - [||]Bar(); - } -}", -@"class C -{ - public C() => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + public C() + { + [||]Bar(); + } + } + """, + """ + class C + { + public C() => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public C() - { - [||]Bar(); - } -}", -@"class C -{ - public C() => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public C() + { + [||]Bar(); + } + } + """, + """ + class C + { + public C() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - public C() - { - return () => { [||] }; - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public C() + { + return () => { [||] }; + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - public C() => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public C() => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public C() => [||]Bar(); -}", -@"class C -{ - public C() - { - Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + public C() => [||]Bar(); + } + """, + """ + class C + { + public C() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public C() => [||]Bar(); -}", -@"class C -{ - public C() - { - Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public C() => [||]Bar(); + } + """, + """ + class C + { + public C() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs index 3a2b87baec824..5b5c446a2e846 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForConversionOperatorsRefactoringTests.cs @@ -38,104 +38,133 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - public static implicit operator bool(C c1) - { - [||]Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public static implicit operator bool(C c1) + { + [||]Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static implicit operator bool(C c1) - { - [||]Bar(); - } -}", -@"class C -{ - public static implicit operator bool(C c1) => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + public static implicit operator bool(C c1) + { + [||]Bar(); + } + } + """, + """ + class C + { + public static implicit operator bool(C c1) => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static implicit operator bool(C c1) - { - [||]Bar(); - } -}", -@"class C -{ - public static implicit operator bool(C c1) => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static implicit operator bool(C c1) + { + [||]Bar(); + } + } + """, + """ + class C + { + public static implicit operator bool(C c1) => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - public static implicit operator bool(C c1) - { - return () => { [||] }; - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static implicit operator bool(C c1) + { + return () => { [||] }; + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - public static implicit operator bool(C c1) => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static implicit operator bool(C c1) => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static implicit operator bool(C c1) => [||]Bar(); -}", -@"class C -{ - public static implicit operator bool(C c1) - { - return Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + public static implicit operator bool(C c1) => [||]Bar(); + } + """, + """ + class C + { + public static implicit operator bool(C c1) + { + return Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static implicit operator bool(C c1) => [||]Bar(); -}", -@"class C -{ - public static implicit operator bool(C c1) - { - return Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public static implicit operator bool(C c1) => [||]Bar(); + } + """, + """ + class C + { + public static implicit operator bool(C c1) + { + return Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs index 32d87cf03a1c3..70d744f4865db 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForIndexersRefactoringTests.cs @@ -39,122 +39,151 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - int this[int i] - { - get - { - [||]return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + int this[int i] + { + get + { + [||]return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int this[int i] - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int this[int i] => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + int this[int i] + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int this[int i] => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int this[int i] - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int this[int i] => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + int this[int i] + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int this[int i] => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - Action Goo[int i] - { - get - { - return () => { [||] }; - } - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + Action Goo[int i] + { + get + { + return () => { [||] }; + } + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - int this[int i] => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + int this[int i] => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int this[int i] => [||]Bar(); -}", -@"class C -{ - int this[int i] - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + int this[int i] => [||]Bar(); + } + """, + """ + class C + { + int this[int i] + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20363")] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int this[int i] => [||]Bar(); -}", -@"class C -{ - int this[int i] - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + int this[int i] => [||]Bar(); + } + """, + """ + class C + { + int this[int i] + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs index 6607e1a53be24..1be89b9af754b 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForLocalFunctionsRefactoringTests.cs @@ -38,121 +38,147 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - void Goo() - { - void Bar() - { - [||]Test(); - } - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + void Goo() + { + void Bar() + { + [||]Test(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - void Bar() - { - [||]Test(); - } - } -}", -@"class C -{ - void Goo() - { - void Bar() => Test(); - } -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + void Goo() + { + void Bar() + { + [||]Test(); + } + } + } + """, + """ + class C + { + void Goo() + { + void Bar() => Test(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - void Bar() - { - [||]Test(); - } - } -}", -@"class C -{ - void Goo() - { - void Bar() => Test(); - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + void Goo() + { + void Bar() + { + [||]Test(); + } + } + } + """, + """ + class C + { + void Goo() + { + void Bar() => Test(); + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - void Goo() - { - void Bar() => [||]Test(); - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + void Goo() + { + void Bar() => [||]Test(); + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - void Bar() => [||]Test(); - } -}", -@"class C -{ - void Goo() - { - void Bar() - { - Test(); - } - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + void Goo() + { + void Bar() => [||]Test(); + } + } + """, + """ + class C + { + void Goo() + { + void Bar() + { + Test(); + } + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - void Bar() => [||]Test(); - } -}", -@"class C -{ - void Goo() - { - void Bar() - { - Test(); - } - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + void Goo() + { + void Bar() => [||]Test(); + } + } + """, + """ + class C + { + void Goo() + { + void Bar() + { + Test(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs index a4d8f846211d0..a967266cb9f8d 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForMethodsRefactoringTests.cs @@ -37,205 +37,260 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - void Goo() - { - [||]Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + void Goo() + { + [||]Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - [||]Bar(); - } -}", -@"class C -{ - void Goo() => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + void Goo() + { + [||]Bar(); + } + } + """, + """ + class C + { + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() - { - [||]Bar(); - } -}", -@"class C -{ - void Goo() => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + void Goo() + { + [||]Bar(); + } + } + """, + """ + class C + { + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - Action Goo() - { - return () => { [||] }; - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + Action Goo() + { + return () => { [||] }; + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - void Goo() => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + void Goo() => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() => [||]Bar(); -}", -@"class C -{ - void Goo() - { - Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + void Goo() => [||]Bar(); + } + """, + """ + class C + { + void Goo() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - void Goo() => [||]Bar(); -}", -@"class C -{ - void Goo() - { - Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + void Goo() => [||]Bar(); + } + """, + """ + class C + { + void Goo() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/25501")] public async Task TestOfferedAtStartOfMethod() { await TestInRegularAndScript1Async( -@"class C -{ - [||]void Goo() - { - Bar(); - } -}", -@"class C -{ - void Goo() => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + [||]void Goo() + { + Bar(); + } + } + """, + """ + class C + { + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/25501")] public async Task TestOfferedBeforeMethodOnSameLine() { await TestInRegularAndScript1Async( -@"class C -{ -[||] void Goo() - { - Bar(); - } -}", -@"class C -{ - void Goo() => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + [||] void Goo() + { + Bar(); + } + } + """, + """ + class C + { + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/25501")] public async Task TestOfferedBeforeAttributes() { await TestInRegularAndScript1Async( -@"class C -{ - [||][A] - void Goo() - { - Bar(); - } -}", -@"class C -{ - [A] - void Goo() => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + [||][A] + void Goo() + { + Bar(); + } + } + """, + """ + class C + { + [A] + void Goo() => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/25501")] public async Task TestNotOfferedBeforeComments() { await TestMissingInRegularAndScriptAsync( -@"class C -{ - [||]/// - void Goo() - { - Bar(); - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + [||]/// + void Goo() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/25501")] public async Task TestNotOfferedInComments() { await TestMissingInRegularAndScriptAsync( -@"class C -{ - /// [||] - void Goo() - { - Bar(); - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + /// [||] + void Goo() + { + Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/53532")] public async Task TestTriviaOnArrow1() { await TestInRegularAndScript1Async( -@"class C -{ - void M() - // Test - [||]=> Console.WriteLine(); -}", -@"class C -{ - void M() - { - // Test - Console.WriteLine(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + void M() + // Test + [||]=> Console.WriteLine(); + } + """, + """ + class C + { + void M() + { + // Test + Console.WriteLine(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs index ff1249eaac430..9ecd34ddde838 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForOperatorsRefactoringTests.cs @@ -38,104 +38,133 @@ private OptionsCollection UseBlockBodyDisabledDiagnostic public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - public static bool operator +(C c1, C c2) - { - [||]Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public static bool operator +(C c1, C c2) + { + [||]Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static bool operator +(C c1, C c2) - { - [||]Bar(); - } -}", -@"class C -{ - public static bool operator +(C c1, C c2) => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); + """ + class C + { + public static bool operator +(C c1, C c2) + { + [||]Bar(); + } + } + """, + """ + class C + { + public static bool operator +(C c1, C c2) => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static bool operator +(C c1, C c2) - { - [||]Bar(); - } -}", -@"class C -{ - public static bool operator +(C c1, C c2) => Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static bool operator +(C c1, C c2) + { + [||]Bar(); + } + } + """, + """ + class C + { + public static bool operator +(C c1, C c2) => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - public static bool operator +(C c1, C c2) - { - return () => { [||] }; - } -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static bool operator +(C c1, C c2) + { + return () => { [||] }; + } + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - public static bool operator +(C c1, C c2) => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBody)); + """ + class C + { + public static bool operator +(C c1, C c2) => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBody)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static bool operator +(C c1, C c2) => [||]Bar(); -}", -@"class C -{ - public static bool operator +(C c1, C c2) - { - return Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); + """ + class C + { + public static bool operator +(C c1, C c2) => [||]Bar(); + } + """, + """ + class C + { + public static bool operator +(C c1, C c2) + { + return Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyDisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - public static bool operator +(C c1, C c2) => [||]Bar(); -}", -@"class C -{ - public static bool operator +(C c1, C c2) - { - return Bar(); - } -}", parameters: new TestParameters(options: UseExpressionBody)); + """ + class C + { + public static bool operator +(C c1, C c2) => [||]Bar(); + } + """, + """ + class C + { + public static bool operator +(C c1, C c2) + { + return Bar(); + } + } + """, + parameters: new TestParameters(options: UseExpressionBody)); } [Fact] diff --git a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs index 8012ce3346234..4db58e84f1612 100644 --- a/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs +++ b/src/Features/CSharpTest/UseExpressionBody/Refactoring/UseExpressionBodyForPropertiesRefactoringTests.cs @@ -77,236 +77,293 @@ private OptionsCollection UseBlockBodyForAccessors_BlockBodyForProperties_Disabl public async Task TestNotOfferedIfUserPrefersExpressionBodiesAndInBlockBody() { await TestMissingAsync( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesWithoutDiagnosticAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestUpdateAccessorIfAccessWantsBlockAndPropertyWantsExpression() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int Goo - { - get => Bar(); - } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int Goo + { + get => Bar(); + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesAndInBlockBody2() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo - { - get - { - [||]return Bar(); - } - } -}", -@"class C -{ - int Goo => Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo + { + get + { + [||]return Bar(); + } + } + } + """, + """ + class C + { + int Goo => Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestNotOfferedInLambda() { await TestMissingAsync( -@"class C -{ - Action Goo - { - get - { - return () => { [||] }; - } - } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + Action Goo + { + get + { + return () => { [||] }; + } + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody() { await TestMissingAsync( -@"class C -{ - int Goo => [||]Bar(); -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestNotOfferedIfUserPrefersBlockBodiesAndInExpressionBody2() { await TestMissingAsync( -@"class C -{ - int Goo => [||]Bar(); -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo => [||]Bar(); -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); } [Fact] public async Task TestOfferedIfUserPrefersBlockBodiesWithoutDiagnosticAndInExpressionBody2() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo => [||]Bar(); -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_BlockBodyForProperties_DisabledDiagnostic)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20363")] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo => [||]Bar(); -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseExpressionBodyForAccessors_ExpressionBodyForProperties)); } [Fact] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody2() { await TestInRegularAndScript1Async( -@"class C -{ - int Goo => [||]Bar(); -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); + """ + class C + { + int Goo => [||]Bar(); + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parameters: new TestParameters(options: UseBlockBodyForAccessors_ExpressionBodyForProperties)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/20360")] public async Task TestOfferedIfUserPrefersExpressionBodiesAndInExpressionBody_CSharp6() { await TestAsync( -@"class C -{ - int Goo => [||]Bar(); -}", -@"class C -{ - int Goo - { - get - { - return Bar(); - } - } -}", parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6), -options: UseExpressionBodyForAccessors_ExpressionBodyForProperties); + """ + class C + { + int Goo => [||]Bar(); + } + """, + """ + class C + { + int Goo + { + get + { + return Bar(); + } + } + } + """, + parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6), + options: UseExpressionBodyForAccessors_ExpressionBodyForProperties); } [Fact]