From 8bdd4972eb6a8768b39bfd7e8732bac856f8ae7f Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Sun, 12 May 2024 19:12:43 +0300 Subject: [PATCH 1/3] Port C# "Invert if" refactoring tests to `VerifyCS` --- .../CSharpInvertIfCodeRefactoringProvider.cs | 11 +- .../InvertIf/InvertIfTests.Elseless.cs | 1223 ++++----- .../CSharpTest/InvertIf/InvertIfTests.cs | 2408 ++++++++--------- 3 files changed, 1778 insertions(+), 1864 deletions(-) diff --git a/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs b/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs index 3daf3339608c8..83eb0d70ac6b4 100644 --- a/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs +++ b/src/Features/CSharp/Portable/InvertIf/CSharpInvertIfCodeRefactoringProvider.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CSharp.Extensions; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Formatting; +using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.InvertIf; using Microsoft.CodeAnalysis.Shared.Extensions; using Microsoft.CodeAnalysis.Text; @@ -21,15 +22,11 @@ namespace Microsoft.CodeAnalysis.CSharp.InvertIf; using static SyntaxFactory; [ExportCodeRefactoringProvider(LanguageNames.CSharp, Name = PredefinedCodeRefactoringProviderNames.InvertIf), Shared] -internal sealed class CSharpInvertIfCodeRefactoringProvider : AbstractInvertIfCodeRefactoringProvider< +[method: ImportingConstructor] +[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] +internal sealed class CSharpInvertIfCodeRefactoringProvider() : AbstractInvertIfCodeRefactoringProvider< SyntaxKind, StatementSyntax, IfStatementSyntax, StatementSyntax> { - [ImportingConstructor] - [SuppressMessage("RoslynDiagnosticsReliability", "RS0033:Importing constructor should be [Obsolete]", Justification = "Used in test code: https://github.com/dotnet/roslyn/issues/42814")] - public CSharpInvertIfCodeRefactoringProvider() - { - } - protected override string GetTitle() => CSharpFeaturesResources.Invert_if; diff --git a/src/Features/CSharpTest/InvertIf/InvertIfTests.Elseless.cs b/src/Features/CSharpTest/InvertIf/InvertIfTests.Elseless.cs index 856f322b68f74..b69f6484d85c0 100644 --- a/src/Features/CSharpTest/InvertIf/InvertIfTests.Elseless.cs +++ b/src/Features/CSharpTest/InvertIf/InvertIfTests.Elseless.cs @@ -3,177 +3,90 @@ // See the LICENSE file in the project root for more information. using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Test.Utilities; using Roslyn.Test.Utilities; using Xunit; -namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.InvertIf +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.InvertIf; + +public partial class InvertIfTests { - [Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)] - public partial class InvertIfTests + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause1() { - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause1() - { - await TestInRegularAndScriptAsync( - """ - class C + await TestAsync(""" + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - if (c) - { - [||]if (c) - { - return 1; - } - } - return 2; - } - } - } - """, - """ - class C - { - void M() - { - switch (o) - { - case 1: - if (c) - { - [||]if (!c) - { - } - else - { - return 1; - } - } - return 2; - } - } - } - """); - } - - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause2() - { - await TestInRegularAndScriptAsync( - """ - class C - { - void M() - { - switch (o) - { - case 1: + case 1: + if (c) + { [||]if (c) { - f(); + return 1; } - g(); - g(); - break; - } + } + return 2; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: + case 1: + if (c) + { [||]if (!c) { } else { - f(); + return 1; } - g(); - g(); - break; - } - } - } - """); - } - - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause3() - { - await TestInRegularAndScriptAsync( - """ - class C - { - void M() - { - [||]if (c) - { - f(); - } - g(); - g(); - } - } - """, - """ - class C - { - void M() - { - if (!c) - { - } - else - { - f(); - } - g(); - g(); + } + return 2; } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause4() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause2() + { + await TestAsync(""" + class C + { + void M() { - bool M() + switch (o) { - if (c) - { + case 1: [||]if (c) { f(); } g(); - } - return false; + g(); + break; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - bool M() + switch (o) { - if (c) - { - if (!c) + case 1: + [||]if (!c) { } else @@ -181,36 +94,74 @@ bool M() f(); } g(); - } - return false; + g(); + break; + } + } + } + """); + } + + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause3() + { + await TestAsync(""" + class C + { + void M() + { + [||]if (c) + { + f(); + } + g(); + g(); + } + } + """, """ + class C + { + void M() + { + if (!c) + { + } + else + { + f(); } + g(); + g(); } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause5() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause4() + { + await TestAsync(""" + class C + { + bool M() { - void M() + if (c) { [||]if (c) { f(); } - - g(); g(); } + return false; } - """, - """ - class C + } + """, """ + class C + { + bool M() { - void M() + if (c) { if (!c) { @@ -219,435 +170,487 @@ void M() { f(); } - - g(); g(); } + return false; + } + } + """); + } + + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause5() + { + await TestAsync(""" + class C + { + void M() + { + [||]if (c) + { + f(); + } + + g(); + g(); } - """); - } + } + """, """ + class C + { + void M() + { + if (!c) + { + } + else + { + f(); + } - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause6() - { - await TestInRegularAndScriptAsync( - """ - class C + g(); + g(); + } + } + """); + } + + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause6() + { + await TestAsync(""" + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - [||]if (c) + case 1: + [||]if (c) + { + if (c) { - if (c) - { - f(); - return 1; - } + f(); + return 1; } + } - f(); - return 2; - } + f(); + return 2; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - [||]if (!c) - { - } - else + case 1: + [||]if (!c) + { + } + else + { + if (c) { - if (c) - { - f(); - return 1; - } + f(); + return 1; } + } - f(); - return 2; - } + f(); + return 2; } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_MoveIfBodyToElseClause7() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_MoveIfBodyToElseClause7() + { + await TestAsync(""" + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - if (c) + case 1: + if (c) + { + [||]if (c) { - [||]if (c) - { - f(); - return 1; - } + f(); + return 1; } + } - f(); - return 2; - } + f(); + return 2; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - if (c) + case 1: + if (c) + { + if (!c) + { + } + else { - if (!c) - { - } - else - { - f(); - return 1; - } + f(); + return 1; } + } - f(); - return 2; - } + f(); + return 2; } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40909")] - public async Task IfWithoutElse_MoveIfBodyToElseClause8() - { - await TestInRegularAndScriptAsync( - """ - using System.Diagnostics; - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40909")] + public async Task IfWithoutElse_MoveIfBodyToElseClause8() + { + await TestAsync(""" + using System.Diagnostics; + class C + { + private static bool IsFalse(bool val) { - private static bool IsFalse(bool val) { + [|if|] (!val) { - [|if|] (!val) - { - return true; - } - Debug.Assert(val); + return true; } - return false; + Debug.Assert(val); } + return false; } - """, - """ - using System.Diagnostics; - class C + } + """, """ + using System.Diagnostics; + class C + { + private static bool IsFalse(bool val) { - private static bool IsFalse(bool val) { + if (val) { - if (val) - { - } - else - { - return true; - } - Debug.Assert(val); } - return false; + else + { + return true; + } + Debug.Assert(val); } + return false; } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody1() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody1() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) + [||]if (!c) + { + continue; + } + // comments + f(); + } + } + } + """, """ + class C + { + void M() + { + foreach (var item in list) + { + [||]if (c) { - [||]if (!c) - { - continue; - } // comments f(); } } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody2() + { + await TestAsync(""" + class C + { + void M() { - void M() + while (c) { - foreach (var item in list) + if (c) { [||]if (c) { - // comments - f(); + continue; } + if (c()) + return; } } } - """); - } - - [Fact] - public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody2() - { - await TestInRegularAndScriptAsync( - """ - class C + } + """, """ + class C + { + void M() { - void M() + while (c) { - while (c) + if (c) { - if (c) + [||]if (!c) { - [||]if (c) - { - continue; - } if (c()) return; } } } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody3() + { + await TestAsync(""" + class C + { + void M() { - void M() + while (c) { - while (c) { - if (c) + [||]if (c) { - [||]if (!c) - { - if (c()) - return; - } + continue; } + if (c()) + return; } } } - """); - } - - [Fact] - public async Task IfWithoutElse_MoveSubsequentStatementsToIfBody3() - { - await TestInRegularAndScriptAsync( - """ - class C + } + """, """ + class C + { + void M() { - void M() + while (c) { - while (c) { + [||]if (!c) { - [||]if (c) - { - continue; - } if (c()) return; } } } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_SwapIfBodyWithSubsequentStatements1() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - while (c) - { - { - [||]if (!c) - { - if (c()) - return; - } - } - } + [||]if (c) + break; + return; } } - """); - } - - [Fact] - public async Task IfWithoutElse_SwapIfBodyWithSubsequentStatements1() - { - await TestInRegularAndScriptAsync( - """ - class C + } + """, """ + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) - { - [||]if (c) - break; + [||]if (!c) return; - } + break; } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_SwapIfBodyWithSubsequentStatements2() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) + [||]if (!c) { - [||]if (!c) - return; - break; + return; } + break; } } - """); - } - - [Fact] - public async Task IfWithoutElse_SwapIfBodyWithSubsequentStatements2() - { - await TestInRegularAndScriptAsync( - """ - class C + } + """, """ + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) + [||]if (c) { - [||]if (!c) - { - return; - } break; } + return; } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_WithElseClause1() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) - { - [||]if (c) - { - break; - } + [||]if (!c) return; - } + f(); } } - """); - } - - [Fact] - public async Task IfWithoutElse_WithElseClause1() - { - await TestInRegularAndScriptAsync( - """ - class C + } + """, """ + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) - { - [||]if (!c) - return; + if (c) f(); - } + else + return; } } - """, - """ - class C + } + """); + } + + [Fact] + public async Task IfWithoutElse_WithNegatedCondition1() + { + await TestAsync(""" + class C + { + void M() + { + [||]if (c) { } + } + } + """, """ + class C + { + void M() { - void M() - { - foreach (var item in list) - { - if (c) - f(); - else - return; - } - } + if (!c) { } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithNegatedCondition1() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithNearmostJumpStatement1() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - [||]if (c) { } + [||]if (c) + { + f(); + } } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + foreach (var item in list) { - if (!c) { } + [||]if (!c) + { + continue; + } + f(); } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithNearmostJumpStatement1() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithNearmostJumpStatement2() + { + await TestAsync(""" + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) { [||]if (c) { @@ -656,13 +659,14 @@ void M() } } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + foreach (var item in list) { - foreach (var item in list) { [||]if (!c) { @@ -672,252 +676,205 @@ void M() } } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithNearmostJumpStatement2() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithNearmostJumpStatement3() + { + await TestAsync(""" + class C + { + void M() { - void M() + [||]if (c) { - foreach (var item in list) - { - { - [||]if (c) - { - f(); - } - } - } + f(); } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + [||]if (!c) { - foreach (var item in list) - { - { - [||]if (!c) - { - continue; - } - f(); - } - } + return; } + f(); } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithNearmostJumpStatement3() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithNearmostJumpStatement4() + { + await TestAsync(""" + class C + { + void M() { - void M() + for (;;) { [||]if (c) { - f(); + break; } } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + for (;;) { [||]if (!c) { - return; + continue; } - f(); + break; } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithNearmostJumpStatement4() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithSubsequentExitPointStatement1() + { + await TestAsync(""" + class C + { + void M() { - void M() + switch (o) { - for (;;) - { + case 1: [||]if (c) { - break; + f(); + f(); } - } + break; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + switch (o) { - for (;;) - { + case 1: [||]if (!c) { - continue; + break; } + f(); + f(); break; - } } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithSubsequentExitPointStatement1() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task IfWithoutElse_WithSubsequentExitPointStatement2() + { + await TestAsync(""" + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - [||]if (c) + case 1: + [||]if (c) + { + if (c) { - f(); - f(); + return 1; } - break; - } + } + + return 2; } } - """, - """ - class C + } + """, """ + class C + { + void M() { - void M() + switch (o) { - switch (o) - { - case 1: - [||]if (!c) - { - break; - } - f(); - f(); - break; - } + case 1: + [||]if (!c) + { + return 2; + } + if (c) + { + return 1; + } + + return 2; } } - """); - } + } + """); + } - [Fact] - public async Task IfWithoutElse_WithSubsequentExitPointStatement2() - { - await TestInRegularAndScriptAsync( - """ - class C + [Theory] + [InlineData("get")] + [InlineData("set")] + [InlineData("init")] + public async Task IfWithoutElse_InPropertyAccessors(string accessor) + { + await TestAsync($$""" + class C + { + private bool _b; + + public string Prop { - void M() + {{accessor}} { - switch (o) + [||]if (_b) { - case 1: - [||]if (c) - { - if (c) - { - return 1; - } - } - - return 2; + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); } } } - """, - """ - class C + } + """, $$""" + class C + { + private bool _b; + + public string Prop { - void M() + {{accessor}} { - switch (o) + if (!_b) { - case 1: - [||]if (!c) - { - return 2; - } - if (c) - { - return 1; - } - - return 2; + return; } + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine(); } } - """); - } - - [Theory] - [InlineData("get")] - [InlineData("set")] - [InlineData("init")] - public async Task IfWithoutElse_InPropertyAccessors(string accessor) - { - await TestInRegularAndScriptAsync( -$@"class C -{{ - private bool _b; - - public string Prop - {{ - {accessor} - {{ - [||]if (_b) - {{ - Console.WriteLine(); - Console.WriteLine(); - Console.WriteLine(); - }} - }} - }} -}}", -$@"class C -{{ - private bool _b; - - public string Prop - {{ - {accessor} - {{ - if (!_b) - {{ - return; - }} - Console.WriteLine(); - Console.WriteLine(); - Console.WriteLine(); - }} - }} -}}"); - } + } + """); } } diff --git a/src/Features/CSharpTest/InvertIf/InvertIfTests.cs b/src/Features/CSharpTest/InvertIf/InvertIfTests.cs index 328d153609067..9dee33f7b2f7e 100644 --- a/src/Features/CSharpTest/InvertIf/InvertIfTests.cs +++ b/src/Features/CSharpTest/InvertIf/InvertIfTests.cs @@ -3,33 +3,28 @@ // See the LICENSE file in the project root for more information. using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CodeRefactorings; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.InvertIf; -using Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings; +using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions; using Microsoft.CodeAnalysis.Test.Utilities; +using Microsoft.CodeAnalysis.Testing; using Roslyn.Test.Utilities; using Xunit; -namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.InvertIf +namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.InvertIf; + +[UseExportProvider, Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)] +public partial class InvertIfTests { - [Trait(Traits.Feature, Traits.Features.CodeActionsInvertIf)] - public partial class InvertIfTests : AbstractCSharpCodeActionTest_NoEditor + private static Task TestInsideMethodAsync( + string initial, + string expected) { - private async Task TestFixOneAsync( - string initial, - string expected) - { - await TestInRegularAndScriptAsync(CreateTreeText(initial), CreateTreeText(expected)); - } - - protected override CodeRefactoringProvider CreateCodeRefactoringProvider(TestWorkspace workspace, TestParameters parameters) - => new CSharpInvertIfCodeRefactoringProvider(); + return TestAsync(CreateTreeText(initial), CreateTreeText(expected)); - private static string CreateTreeText(string initial) + static string CreateTreeText(string initial) { - return - """ + return $$""" class A { bool a = true; @@ -39,428 +34,499 @@ class A void Goo() { - """ + initial + """ + {{initial}} } } """; } + } - [Fact] - public async Task TestSingleLine_Identifier() + private static async Task TestAsync(string initial, string expected, LanguageVersion languageVersion = LanguageVersion.Latest) + { + await new CSharpCodeRefactoringVerifier.Test { - await TestFixOneAsync( + TestCode = initial, + FixedCode = expected, + LanguageVersion = languageVersion, + CompilerDiagnostics = CompilerDiagnostics.None, + }.RunAsync(); + } + + [Fact] + public async Task TestSingleLine_Identifier() + { + await TestInsideMethodAsync( @"[||]if (a) { a(); } else { b(); }", @"if (!a) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_IdentifierWithTrivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_IdentifierWithTrivia() + { + await TestInsideMethodAsync( @"[||]if /*0*/(/*1*/a/*2*/)/*3*/ { a(); } else { b(); }", @"if /*0*/(/*1*/!a/*2*/)/*3*/ { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_NotIdentifier() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_NotIdentifier() + { + await TestInsideMethodAsync( @"[||]if (!a) { a(); } else { b(); }", @"if (a) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_NotIdentifierWithTrivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_NotIdentifierWithTrivia() + { + await TestInsideMethodAsync( @"[||]if /*0*/(/*1*/!/*1b*/a/*2*/)/*3*/ { a(); } else { b(); }", @"if /*0*/(/*1*/a/*2*/)/*3*/ { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_EqualsEquals() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_EqualsEquals() + { + await TestInsideMethodAsync( @"[||]if (a == b) { a(); } else { b(); }", @"if (a != b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_NotEquals() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_NotEquals() + { + await TestInsideMethodAsync( @"[||]if (a != b) { a(); } else { b(); }", @"if (a == b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_GreaterThan() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_GreaterThan() + { + await TestInsideMethodAsync( @"[||]if (a > b) { a(); } else { b(); }", @"if (a <= b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_GreaterThanEquals() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_GreaterThanEquals() + { + await TestInsideMethodAsync( @"[||]if (a >= b) { a(); } else { b(); }", @"if (a < b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_LessThan() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_LessThan() + { + await TestInsideMethodAsync( @"[||]if (a < b) { a(); } else { b(); }", @"if (a >= b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_LessThanEquals() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_LessThanEquals() + { + await TestInsideMethodAsync( @"[||]if (a <= b) { a(); } else { b(); }", @"if (a > b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_DoubleParentheses() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_DoubleParentheses() + { + await TestInsideMethodAsync( @"[||]if ((a)) { a(); } else { b(); }", @"if (!a) { b(); } else { a(); }"); - } + } - [Fact(Skip = "https://github.com/dotnet/roslyn/issues/26427")] - public async Task TestSingleLine_DoubleParenthesesWithInnerTrivia() - { - await TestFixOneAsync( + [Fact(Skip = "https://github.com/dotnet/roslyn/issues/26427")] + public async Task TestSingleLine_DoubleParenthesesWithInnerTrivia() + { + await TestInsideMethodAsync( @"[||]if ((/*1*/a/*2*/)) { a(); } else { b(); }", @"if (/*1*/!a/*2*/) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_DoubleParenthesesWithMiddleTrivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_DoubleParenthesesWithMiddleTrivia() + { + await TestInsideMethodAsync( @"[||]if (/*1*/(a)/*2*/) { a(); } else { b(); }", @"if (/*1*/!a/*2*/) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_DoubleParenthesesWithOutsideTrivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_DoubleParenthesesWithOutsideTrivia() + { + await TestInsideMethodAsync( @"[||]if /*before*/((a))/*after*/ { a(); } else { b(); }", @"if /*before*/(!a)/*after*/ { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Is() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Is() + { + await TestInsideMethodAsync( @"[||]if (a is Goo) { a(); } else { b(); }", @"if (a is not Goo) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_MethodCall() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_MethodCall() + { + await TestInsideMethodAsync( @"[||]if (a.Goo()) { a(); } else { b(); }", @"if (!a.Goo()) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Or() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Or() + { + await TestInsideMethodAsync( @"[||]if (a || b) { a(); } else { b(); }", @"if (!a && !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Or2() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Or2() + { + await TestInsideMethodAsync( @"[||]if (!a || !b) { a(); } else { b(); }", @"if (a && b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Or3() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Or3() + { + await TestInsideMethodAsync( @"[||]if (!a || b) { a(); } else { b(); }", @"if (a && !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Or4() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Or4() + { + await TestInsideMethodAsync( @"[||]if (a | b) { a(); } else { b(); }", @"if (!a & !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_And() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_And() + { + await TestInsideMethodAsync( @"[||]if (a && b) { a(); } else { b(); }", @"if (!a || !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_And2() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_And2() + { + await TestInsideMethodAsync( @"[||]if (!a && !b) { a(); } else { b(); }", @"if (a || b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_And3() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_And3() + { + await TestInsideMethodAsync( @"[||]if (!a && b) { a(); } else { b(); }", @"if (a || !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_And4() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_And4() + { + await TestInsideMethodAsync( @"[||]if (a & b) { a(); } else { b(); }", @"if (!a | !b) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_ParenthesizeAndForPrecedence() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_ParenthesizeAndForPrecedence() + { + await TestInsideMethodAsync( @"[||]if (a && b || c) { a(); } else { b(); }", @"if ((!a || !b) && !c) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Plus() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Plus() + { + await TestInsideMethodAsync( @"[||]if (a + b) { a(); } else { b(); }", @"if (!(a + b)) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_True() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_True() + { + await TestInsideMethodAsync( @"[||]if (true) { a(); } else { b(); }", @"if (false) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_TrueWithTrivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_TrueWithTrivia() + { + await TestInsideMethodAsync( @"[||]if (/*1*/true/*2*/) { a(); } else { b(); }", @"if (/*1*/false/*2*/) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_False() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_False() + { + await TestInsideMethodAsync( @"[||]if (false) { a(); } else { b(); }", @"if (true) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_OtherLiteralExpression() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_OtherLiteralExpression() + { + await TestInsideMethodAsync( @"[||]if (literalexpression) { a(); } else { b(); }", @"if (!literalexpression) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_TrueAndFalse() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_TrueAndFalse() + { + await TestInsideMethodAsync( @"[||]if (true && false) { a(); } else { b(); }", @"if (false || true) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_NoCurlyBraces() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_NoCurlyBraces() + { + await TestInsideMethodAsync( @"[||]if (a) a(); else b();", @"if (!a) b(); else a();"); - } + } - [Fact] - public async Task TestSingleLine_CurlyBracesOnIf() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_CurlyBracesOnIf() + { + await TestInsideMethodAsync( @"[||]if (a) { a(); } else b();", @"if (!a) b(); else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_CurlyBracesOnElse() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_CurlyBracesOnElse() + { + await TestInsideMethodAsync( @"[||]if (a) a(); else { b(); }", @"if (!a) { b(); } else a();"); - } + } - [Fact] - public async Task TestSingleLine_IfElseIf() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_IfElseIf() + { + await TestInsideMethodAsync( @"[||]if (a) { a(); } else if (b) { b(); }", @"if (!a) { if (b) { b(); } } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_IfElseIfElse() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_IfElseIfElse() + { + await TestInsideMethodAsync( @"[||]if (a) { a(); } else if (b) { b(); } else { c(); }", @"if (!a) { if (b) { b(); } else { c(); } } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_CompoundConditional() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_CompoundConditional() + { + await TestInsideMethodAsync( @"[||]if (((a == b) && (c != d)) || ((e < f) && (!g))) { a(); } else { b(); }", @"if ((a != b || c == d) && (e >= f || g)) { b(); } else { a(); }"); - } + } - [Fact] - public async Task TestSingleLine_Trivia() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_Trivia() + { + await TestInsideMethodAsync( @"[||]if /*1*/ (a) /*2*/ { /*3*/ a() /*4*/; /*5*/ } /*6*/ else if /*7*/ (b) /*8*/ { /*9*/ b(); /*10*/ } /*11*/ else /*12*/ { /*13*/ c(); /*14*/} /*15*/", @"if /*1*/ (!a) /*2*/ { if /*7*/ (b) /*8*/ { /*9*/ b(); /*10*/ } /*11*/ else /*12*/ { /*13*/ c(); /*14*/} /*6*/ } else { /*3*/ a() /*4*/; /*5*/ } /*15*/"); - } + } - [Fact] - public async Task TestKeepTriviaWithinExpression_BrokenCode() - { - await TestInRegularAndScriptAsync( - """ - class A + [Fact] + public async Task TestKeepTriviaWithinExpression_BrokenCode() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() + [||]if (a || + b && + c < // comment + d) { - [||]if (a || - b && - c < // comment - d) - { - a(); - } - else - { - b(); - } + a(); + } + else + { + b(); } } - """, - """ - class A + } + """, """ + class A + { + void Goo() { - void Goo() + if (!a && + (!b || + c >= // comment + d)) { - if (!a && - (!b || - c >= // comment - d)) - { - b(); - } - else - { - a(); - } + b(); + } + else + { + a(); } } - """); - } + } + """); + } - [Fact] - public async Task TestKeepTriviaWithinExpression() - { - await TestInRegularAndScriptAsync( - """ - class A + [Fact] + public async Task TestKeepTriviaWithinExpression() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() + bool a = true; + bool b = true; + bool c = true; + bool d = true; + + [||]if (a || + b && + c < // comment + d) { - bool a = true; - bool b = true; - bool c = true; - bool d = true; - - [||]if (a || - b && - c < // comment - d) - { - a(); - } - else - { - b(); - } + a(); + } + else + { + b(); } } - """, - """ - class A + } + """, """ + class A + { + void Goo() { - void Goo() + bool a = true; + bool b = true; + bool c = true; + bool d = true; + + if (!a && + (!b || + c >= // comment + d)) + { + b(); + } + else { - bool a = true; - bool b = true; - bool c = true; - bool d = true; + a(); + } + } + } + """); + } - if (!a && - (!b || - c >= // comment - d)) + [Fact] + public async Task TestMultiline_IfElseIfElse() + { + await TestAsync(""" + class A + { + void Goo() + { + [||]if (a) + { + a(); + } + else if (b) + { + b(); + } + else + { + c(); + } + } + } + """, """ + class A + { + void Goo() + { + if (!a) + { + if (b) { b(); } else { - a(); + c(); } } + else + { + a(); + } } - """); - } + } + """); + } - [Fact] - public async Task TestMultiline_IfElseIfElse() - { - await TestInRegularAndScriptAsync( - """ - class A + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] + public async Task TestMultiline_IfElseIfElseSelection1() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() + [|if (a) { - [||]if (a) - { - a(); - } - else if (b) + a(); + } + else if (b) + { + b(); + } + else + { + c(); + }|] + } + } + """, """ + class A + { + void Goo() + { + if (!a) + { + if (b) { b(); } @@ -469,245 +535,195 @@ void Goo() c(); } } + else + { + a(); + } } - """, - """ - class A + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] + public async Task TestMultiline_IfElseIfElseSelection2() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() + [|if (a) + { + a(); + }|] + else if (b) { - if (!a) + b(); + } + else + { + c(); + } + } + } + """, """ + class A + { + void Goo() + { + if (!a) + { + if (b) { - if (b) - { - b(); - } - else - { - c(); - } + b(); } else { - a(); + c(); } } + else + { + a(); + } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] - public async Task TestMultiline_IfElseIfElseSelection1() - { - await TestInRegularAndScriptAsync( - """ - class A + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] + public async Task TestMultilineMissing_IfElseIfElseSubSelection() + { + var code = """ + class A + { + void Goo() { - void Goo() + if (a) { - [|if (a) - { - a(); - } - else if (b) - { - b(); - } - else - { - c(); - }|] + a(); } - } - """, - """ - class A - { - void Goo() + [|else if (b) { - if (!a) - { - if (b) - { - b(); - } - else - { - c(); - } - } - else - { - a(); - } + b(); } + else + { + c(); + }|] } - """); - } + } + """; - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] - public async Task TestMultiline_IfElseIfElseSelection2() - { - await TestInRegularAndScriptAsync( - """ - class A + await TestAsync(code, code); + } + + [Fact] + public async Task TestMultiline_IfElse() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() - { - [|if (a) - { - a(); - }|] - else if (b) - { - b(); - } - else - { - c(); - } - } + [||]if (foo) + bar(); + else + if (baz) + Quux(); } - """, - """ - class A + } + """, """ + class A + { + void Goo() { - void Goo() + if (!foo) { - if (!a) - { - if (b) - { - b(); - } - else - { - c(); - } - } - else - { - a(); - } + if (baz) + Quux(); } + else + bar(); } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/35525")] - public async Task TestMultilineMissing_IfElseIfElseSubSelection() - { - await TestMissingInRegularAndScriptAsync( - """ - class A + [Fact] + public async Task TestMultiline_OpenCloseBracesSameLine() + { + await TestAsync(""" + class A + { + void Goo() { - void Goo() - { - if (a) - { - a(); - } - [|else if (b) - { - b(); - } - else - { - c(); - }|] + [||]if (foo) { + x(); + x(); + } else { + y(); + y(); } } - """); - } - - [Fact] - public async Task TestMultiline_IfElse() - { - await TestInRegularAndScriptAsync( - """ - class A + } + """, """ + class A + { + void Goo() { - void Goo() + if (!foo) { - [||]if (foo) - bar(); - else - if (baz) - Quux(); + y(); + y(); } - } - """, - """ - class A - { - void Goo() + else { - if (!foo) - { - if (baz) - Quux(); - } - else - bar(); + x(); + x(); } } - """); - } + } + """); + } - [Fact] - public async Task TestMultiline_OpenCloseBracesSameLine() - { - await TestInRegularAndScriptAsync( - """ - class A - { - void Goo() - { - [||]if (foo) { - x(); - x(); - } else { - y(); - y(); - } - } + [Fact] + public async Task TestMultiline_Trivia() + { + await TestAsync(""" + class A + { + void Goo() + { /*1*/ + [||]if (a) /*2*/ + { /*3*/ + /*4*/ + goo(); /*5*/ + /*6*/ + } /*7*/ + else if (b) /*8*/ + { /*9*/ + /*10*/ + goo(); /*11*/ + /*12*/ + } /*13*/ + else /*14*/ + { /*15*/ + /*16*/ + goo(); /*17*/ + /*18*/ + } /*19*/ + /*20*/ } - """, - """ - class A - { - void Goo() + } + """, """ + class A + { + void Goo() + { /*1*/ + if (!a) /*2*/ { - if (!foo) - { - y(); - y(); - } - else - { - x(); - x(); - } - } - } - """); - } - [Fact] - public async Task TestMultiline_Trivia() - { - await TestInRegularAndScriptAsync( - """ - class A - { - void Goo() - { /*1*/ - [||]if (a) /*2*/ - { /*3*/ - /*4*/ - goo(); /*5*/ - /*6*/ - } /*7*/ - else if (b) /*8*/ + if (b) /*8*/ { /*9*/ /*10*/ goo(); /*11*/ @@ -719,963 +735,907 @@ void Goo() goo(); /*17*/ /*18*/ } /*19*/ - /*20*/ } + else + { /*3*/ + /*4*/ + goo(); /*5*/ + /*6*/ + } /*7*/ + /*20*/ } - """, - """ - class A - { - void Goo() - { /*1*/ - if (!a) /*2*/ - { - if (b) /*8*/ - { /*9*/ - /*10*/ - goo(); /*11*/ - /*12*/ - } /*13*/ - else /*14*/ - { /*15*/ - /*16*/ - goo(); /*17*/ - /*18*/ - } /*19*/ - } - else - { /*3*/ - /*4*/ - goo(); /*5*/ - /*6*/ - } /*7*/ - /*20*/ - } - } - """); - } + } + """); + } - [Fact] - public async Task TestOverlapsHiddenPosition1() - { - await TestMissingInRegularAndScriptAsync( - """ - class C + [Fact] + public async Task TestOverlapsHiddenPosition1() + { + var code = """ + class C + { + void F() { - void F() + #line hidden + [||]if (a) { - #line hidden - [||]if (a) - { - a(); - } - else - { - b(); - } - #line default + a(); + } + else + { + b(); } + #line default } - """); - } + } + """; - [Fact] - public async Task TestOverlapsHiddenPosition2() - { - await TestMissingInRegularAndScriptAsync( - """ - class C + await TestAsync(code, code); + } + + [Fact] + public async Task TestOverlapsHiddenPosition2() + { + var code = """ + class C + { + void F() { - void F() + [||]if (a) { - [||]if (a) - { - #line hidden - a(); - #line default - } - else - { - b(); - } + #line hidden + a(); + #line default + } + else + { + b(); } } - """); - } + } + """; - [Fact] - public async Task TestOverlapsHiddenPosition3() - { - await TestMissingInRegularAndScriptAsync( - """ - class C + await TestAsync(code, code); + } + + [Fact] + public async Task TestOverlapsHiddenPosition3() + { + var code = """ + class C + { + void F() { - void F() + [||]if (a) { - [||]if (a) - { - a(); - } - else - { - #line hidden - b(); - #line default - } + a(); + } + else + { + #line hidden + b(); + #line default } } - """); - } + } + """; - [Fact] - public async Task TestOverlapsHiddenPosition4() - { - await TestMissingInRegularAndScriptAsync( - """ - class C + await TestAsync(code, code); + } + + [Fact] + public async Task TestOverlapsHiddenPosition4() + { + var code = """ + class C + { + void F() { - void F() + [||]if (a) { - [||]if (a) - { - #line hidden - a(); - } - else - { - b(); - #line default - } + #line hidden + a(); + } + else + { + b(); + #line default } } - """); - } + } + """; - [Fact] - public async Task TestOverlapsHiddenPosition5() - { - await TestMissingInRegularAndScriptAsync( - """ - class C + await TestAsync(code, code); + } + + [Fact] + public async Task TestOverlapsHiddenPosition5() + { + var code = """ + class C + { + void F() { - void F() + [||]if (a) { - [||]if (a) - { - a(); - #line hidden - } - else - { - #line default - b(); - } + a(); + #line hidden + } + else + { + #line default + b(); } } - """); - } + } + """; - [Fact] - public async Task TestOverlapsHiddenPosition6() - { - await TestInRegularAndScriptAsync( - """ - #line hidden - class C + await TestAsync(code, code); + } + + [Fact] + public async Task TestOverlapsHiddenPosition6() + { + await TestAsync(""" + #line hidden + class C + { + void F() { - void F() + #line default + [||]if (a) { - #line default - [||]if (a) - { - a(); - } - else - { - b(); - } + a(); + } + else + { + b(); } } - """, - - """ - #line hidden - class C + } + """, """ + #line hidden + class C + { + void F() { - void F() + #line default + if (!a) { - #line default - if (!a) - { - b(); - } - else - { - a(); - } + b(); + } + else + { + a(); } } - """); - } + } + """); - [Fact] - public async Task TestOverlapsHiddenPosition7() - { - await TestInRegularAndScriptAsync( - """ - #line hidden - class C + } + + [Fact] + public async Task TestOverlapsHiddenPosition7() + { + await TestAsync(""" + #line hidden + class C + { + void F() { - void F() + #line default + [||]if (a) { - #line default - [||]if (a) - { - a(); - } - else - { - b(); - } - #line hidden + a(); + } + else + { + b(); } + #line hidden } - #line default - """, - - """ - #line hidden - class C + } + #line default + """, """ + #line hidden + class C + { + void F() { - void F() + #line default + if (!a) { - #line default - if (!a) - { - b(); - } - else - { - a(); - } - #line hidden + b(); + } + else + { + a(); } + #line hidden } - #line default - """); - } + } + #line default + """); + } - [Fact] - public async Task TestSingleLine_SimplifyToLengthEqualsZero() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_SimplifyToLengthEqualsZero() + { + await TestInsideMethodAsync( @"string x; [||]if (x.Length > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ", @"string x; if (x.Length == 0) { EqualsZero(); } else { GreaterThanZero(); } } } "); - } + } - [Fact] - public async Task TestSingleLine_SimplifyToLengthEqualsZero2() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_SimplifyToLengthEqualsZero2() + { + await TestInsideMethodAsync( @"string[] x; [||]if (x.Length > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ", @"string[] x; if (x.Length == 0) { EqualsZero(); } else { GreaterThanZero(); } } } "); - } + } - [Fact] - public async Task TestSingleLine_SimplifyToLengthEqualsZero3() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_SimplifyToLengthEqualsZero3() + { + await TestInsideMethodAsync( @"string x; [||]if (x.Length > 0x0) { a(); } else { b(); } } } ", @"string x; if (x.Length == 0x0) { b(); } else { a(); } } } "); - } + } - [Fact] - public async Task TestSingleLine_SimplifyToLengthEqualsZero4() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_SimplifyToLengthEqualsZero4() + { + await TestInsideMethodAsync( @"string x; [||]if (0 < x.Length) { a(); } else { b(); } } } ", @"string x; if (0 == x.Length) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToEqualsZero1() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToEqualsZero1() + { + await TestInsideMethodAsync( @"byte x = 1; [||]if (0 < x) { a(); } else { b(); } } } ", @"byte x = 1; if (0 == x) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToEqualsZero2() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToEqualsZero2() + { + await TestInsideMethodAsync( @"ushort x = 1; [||]if (0 < x) { a(); } else { b(); } } } ", @"ushort x = 1; if (0 == x) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToEqualsZero3() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToEqualsZero3() + { + await TestInsideMethodAsync( @"uint x = 1; [||]if (0 < x) { a(); } else { b(); } } } ", @"uint x = 1; if (0 == x) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToEqualsZero4() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToEqualsZero4() + { + await TestInsideMethodAsync( @"ulong x = 1; [||]if (x > 0) { a(); } else { b(); } } } ", @"ulong x = 1; if (x == 0) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToNotEqualsZero1() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToNotEqualsZero1() + { + await TestInsideMethodAsync( @"ulong x = 1; [||]if (0 == x) { a(); } else { b(); } } } ", @"ulong x = 1; if (0 != x) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] - public async Task TestSingleLine_SimplifyToNotEqualsZero2() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545986")] + public async Task TestSingleLine_SimplifyToNotEqualsZero2() + { + await TestInsideMethodAsync( @"ulong x = 1; [||]if (x == 0) { a(); } else { b(); } } } ", @"ulong x = 1; if (x != 0) { b(); } else { a(); } } } "); - } + } - [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530505")] - public async Task TestSingleLine_SimplifyLongLengthEqualsZero() - { - await TestFixOneAsync( + [Fact, WorkItem("http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530505")] + public async Task TestSingleLine_SimplifyLongLengthEqualsZero() + { + await TestInsideMethodAsync( @"string[] x; [||]if (x.LongLength > 0) { GreaterThanZero(); } else { EqualsZero(); } } } ", @"string[] x; if (x.LongLength == 0) { EqualsZero(); } else { GreaterThanZero(); } } } "); - } + } - [Fact] - public async Task TestSingleLine_DoesNotSimplifyToLengthEqualsZero() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_DoesNotSimplifyToLengthEqualsZero() + { + await TestInsideMethodAsync( @"string x; [||]if (x.Length >= 0) { a(); } else { b(); } } } ", @"string x; if (x.Length < 0) { b(); } else { a(); } } } "); - } + } - [Fact] - public async Task TestSingleLine_DoesNotSimplifyToLengthEqualsZero2() - { - await TestFixOneAsync( + [Fact] + public async Task TestSingleLine_DoesNotSimplifyToLengthEqualsZero2() + { + await TestInsideMethodAsync( @"string x; [||]if (x.Length > 0.0f) { GreaterThanZero(); } else { EqualsZero(); } } } ", @"string x; if (x.Length <= 0.0f) { EqualsZero(); } else { GreaterThanZero(); } } } "); - } + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/29434")] - public async Task TestIsExpression() - { - await TestInRegularAndScriptAsync( + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/29434")] + public async Task TestIsExpression() + { + await TestAsync( @"class C { void M(object o) { [||]if (o is C) { a(); } else { } } }", @"class C { void M(object o) { if (o is not C) { } else { a(); } } }"); - } + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] - public async Task TestEmptyIf() - { - await TestInRegularAndScriptAsync( - @"class C { void M(string s){ [||]if (s == ""a""){}else{ s = ""b""}}}", - @"class C { void M(string s){ if (s != ""a"") { s = ""b""}}}"); - } + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] + public async Task TestEmptyIf() + { + await TestAsync( + @"class C { void M(string s){ [||]if (s == ""a""){}else{ s = ""b""}}}", + @"class C { void M(string s){ if (s != ""a"") { s = ""b""}}}"); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] - public async Task TestOnlySingleLineCommentIf() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] + public async Task TestOnlySingleLineCommentIf() + { + await TestAsync(""" + class C + { + void M(string s) { - void M(string s) + [||]if (s == "a") { - [||]if (s == "a") - { - // A single line comment - } - else - { - s = "b" - } + // A single line comment + } + else + { + s = "b" } } - """, - """ - class C + } + """, """ + class C + { + void M(string s) { - void M(string s) + if (s != "a") { - if (s != "a") - { - s = "b" - } - else - { - // A single line comment - } + s = "b" + } + else + { + // A single line comment } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] - public async Task TestOnlyMultilineLineCommentIf() - { - await TestInRegularAndScriptAsync( - """ - class C - { - void M(string s) + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/43224")] + public async Task TestOnlyMultilineLineCommentIf() + { + await TestAsync(""" + class C + { + void M(string s) + { + [||]if (s == "a") { - [||]if (s == "a") - { - /* - * This is - * a multiline - * comment with - * two words - * per line. - */ - } - else - { - s = "b" - } + /* + * This is + * a multiline + * comment with + * two words + * per line. + */ + } + else + { + s = "b" } } - """, - """ - class C - { - void M(string s) + } + """, """ + class C + { + void M(string s) + { + if (s != "a") { - if (s != "a") - { - s = "b" - } - else - { - /* - * This is - * a multiline - * comment with - * two words - * per line. - */ - } + s = "b" + } + else + { + /* + * This is + * a multiline + * comment with + * two words + * per line. + */ } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] - public async Task TestIsCheck_CSharp6() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] + public async Task TestIsCheck_CSharp6() + { + await TestAsync(""" + class C + { + int M() { - int M() + [||]if (c is object) { - [||]if (c is object) - { - return 1; - } - else - { - return 2; - } + return 1; + } + else + { + return 2; } } - """, - """ - class C + } + """, """ + class C + { + int M() { - int M() + if (!(c is object)) { - if (!(c is object)) - { - return 2; - } - else - { - return 1; - } + return 2; + } + else + { + return 1; } } - """, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp6)); - } + } + """, LanguageVersion.CSharp6); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] - public async Task TestIsCheck_CSharp8() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] + public async Task TestIsCheck_CSharp8() + { + await TestAsync(""" + class C + { + int M() { - int M() + [||]if (c is object) { - [||]if (c is object) - { - return 1; - } - else - { - return 2; - } + return 1; + } + else + { + return 2; } } - """, - """ - class C + } + """, """ + class C + { + int M() { - int M() + if (c is null) { - if (c is null) - { - return 2; - } - else - { - return 1; - } + return 2; + } + else + { + return 1; } } - """, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)); - } + } + """, LanguageVersion.CSharp8); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] - public async Task TestIsCheck_CSharp9() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] + public async Task TestIsCheck_CSharp9() + { + await TestAsync(""" + class C + { + int M() { - int M() + [||]if (c is object) { - [||]if (c is object) - { - return 1; - } - else - { - return 2; - } + return 1; + } + else + { + return 2; } } - """, - """ - class C + } + """, """ + class C + { + int M() { - int M() + if (c is null) { - if (c is null) - { - return 2; - } - else - { - return 1; - } + return 2; + } + else + { + return 1; } } - """, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9)); - } + } + """, LanguageVersion.CSharp9); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] - public async Task TestIsNotObjectCheck_CSharp8() - { - // Not terrific. But the starting code is not legal C#8 either. In this case because we don't even support - // 'not' patterns wee dont' bother diving into the pattern to negate it, and we instead just negate the - // expression. - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] + public async Task TestIsNotObjectCheck_CSharp8() + { + // Not terrific. But the starting code is not legal C#8 either. In this case because we don't even support + // 'not' patterns wee don't bother diving into the pattern to negate it, and we instead just negate the + // expression. + await TestAsync(""" + class C + { + int M() { - int M() + [||]if (c is not object) { - [||]if (c is not object) - { - return 1; - } - else - { - return 2; - } + return 1; + } + else + { + return 2; } } - """, - """ - class C + } + """, """ + class C + { + int M() { - int M() + if (c is object) { - if (c is object) - { - return 2; - } - else - { - return 1; - } + return 2; + } + else + { + return 1; } } - """, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp8)); - } + } + """, LanguageVersion.CSharp8); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] - public async Task TestIsNotObjectCheck_CSharp9() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/51359")] + public async Task TestIsNotObjectCheck_CSharp9() + { + await TestAsync(""" + class C + { + int M() { - int M() + [||]if (c is not object) { - [||]if (c is not object) - { - return 1; - } - else - { - return 2; - } + return 1; + } + else + { + return 2; } } - """, - """ - class C + } + """, """ + class C + { + int M() { - int M() + if (c is not null) { - if (c is not null) - { - return 2; - } - else - { - return 1; - } + return 2; + } + else + { + return 1; } } - """, parseOptions: CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9)); - } + } + """, LanguageVersion.CSharp9); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] - public async Task TestLiftedNullable_GreaterThan() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] + public async Task TestLiftedNullable_GreaterThan() + { + await TestAsync(""" + class C + { + void M(int? p) { - void M(int? p) + [||]if (p > 10) { - [||]if (p > 10) - { - System.Console.WriteLine("p is not null and p.Value > 10"); - } + System.Console.WriteLine("p is not null and p.Value > 10"); } } - """, - """ - class C + } + """, """ + class C + { + void M(int? p) { - void M(int? p) + if (!(p > 10)) { - if (!(p > 10)) - { - return; - } - System.Console.WriteLine("p is not null and p.Value > 10"); + return; } + System.Console.WriteLine("p is not null and p.Value > 10"); } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] - public async Task TestLiftedNullable_GreaterThanOrEqual() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] + public async Task TestLiftedNullable_GreaterThanOrEqual() + { + await TestAsync(""" + class C + { + void M(int? p) { - void M(int? p) + [||]if (p >= 10) { - [||]if (p >= 10) - { - System.Console.WriteLine("p is not null and p.Value >= 10"); - } + System.Console.WriteLine("p is not null and p.Value >= 10"); } } - """, - """ - class C + } + """, """ + class C + { + void M(int? p) { - void M(int? p) + if (!(p >= 10)) { - if (!(p >= 10)) - { - return; - } - System.Console.WriteLine("p is not null and p.Value >= 10"); + return; } + System.Console.WriteLine("p is not null and p.Value >= 10"); } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] - public async Task TestLiftedNullable_LessThan() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] + public async Task TestLiftedNullable_LessThan() + { + await TestAsync(""" + class C + { + void M(int? p) { - void M(int? p) + [||]if (p < 10) { - [||]if (p < 10) - { - System.Console.WriteLine("p is not null and p.Value < 10"); - } + System.Console.WriteLine("p is not null and p.Value < 10"); } } - """, - """ - class C + } + """, """ + class C + { + void M(int? p) { - void M(int? p) + if (!(p < 10)) { - if (!(p < 10)) - { - return; - } - System.Console.WriteLine("p is not null and p.Value < 10"); + return; } + System.Console.WriteLine("p is not null and p.Value < 10"); } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] - public async Task TestLiftedNullable_LessThanOrEqual() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] + public async Task TestLiftedNullable_LessThanOrEqual() + { + await TestAsync(""" + class C + { + void M(int? p) { - void M(int? p) + [||]if (p <= 10) { - [||]if (p <= 10) - { - System.Console.WriteLine("p is not null and p.Value <= 10"); - } + System.Console.WriteLine("p is not null and p.Value <= 10"); } } - """, - """ - class C + } + """, """ + class C + { + void M(int? p) { - void M(int? p) + if (!(p <= 10)) { - if (!(p <= 10)) - { - return; - } - System.Console.WriteLine("p is not null and p.Value <= 10"); + return; } + System.Console.WriteLine("p is not null and p.Value <= 10"); } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] - public async Task TestNullableReference_GreaterThan() - { - await TestInRegularAndScriptAsync( - """ - #nullable enable - using System; - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/63311")] + public async Task TestNullableReference_GreaterThan() + { + await TestAsync(""" + #nullable enable + using System; + class C + { + void M(C? p) { - void M(C? p) + [||]if (p > new C()) { - [||]if (p > new C()) - { - Console.WriteLine("Null-handling semantics may actually change depending on the operator implementation"); - } + Console.WriteLine("Null-handling semantics may actually change depending on the operator implementation"); } - - public static bool operator <(C? left, C? right) => throw new NotImplementedException(); - public static bool operator >(C? left, C? right) => throw new NotImplementedException(); - public static bool operator <=(C? left, C? right) => throw new NotImplementedException(); - public static bool operator >=(C? left, C? right) => throw new NotImplementedException(); } - """, - """ - #nullable enable - using System; - class C + + public static bool operator <(C? left, C? right) => throw new NotImplementedException(); + public static bool operator >(C? left, C? right) => throw new NotImplementedException(); + public static bool operator <=(C? left, C? right) => throw new NotImplementedException(); + public static bool operator >=(C? left, C? right) => throw new NotImplementedException(); + } + """, """ + #nullable enable + using System; + class C + { + void M(C? p) { - void M(C? p) + if (p <= new C()) { - if (p <= new C()) - { - return; - } - Console.WriteLine("Null-handling semantics may actually change depending on the operator implementation"); + return; } - - public static bool operator <(C? left, C? right) => throw new NotImplementedException(); - public static bool operator >(C? left, C? right) => throw new NotImplementedException(); - public static bool operator <=(C? left, C? right) => throw new NotImplementedException(); - public static bool operator >=(C? left, C? right) => throw new NotImplementedException(); + Console.WriteLine("Null-handling semantics may actually change depending on the operator implementation"); } - """); - } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40585")] - public async Task TestYieldBreak() - { - await TestInRegularAndScriptAsync( - """ - using System.Collections; + public static bool operator <(C? left, C? right) => throw new NotImplementedException(); + public static bool operator >(C? left, C? right) => throw new NotImplementedException(); + public static bool operator <=(C? left, C? right) => throw new NotImplementedException(); + public static bool operator >=(C? left, C? right) => throw new NotImplementedException(); + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/40585")] + public async Task TestYieldBreak() + { + await TestAsync(""" + using System.Collections; - class Program + class Program + { + public static IEnumerable Method(bool condition) { - public static IEnumerable Method(bool condition) + [||]if (condition) { - [||]if (condition) - { - yield return 1; - } + yield return 1; } } - """, - """ - using System.Collections; + } + """, """ + using System.Collections; - class Program + class Program + { + public static IEnumerable Method(bool condition) { - public static IEnumerable Method(bool condition) + if (!condition) { - if (!condition) - { - yield break; - } - yield return 1; + yield break; } + yield return 1; } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] - public async Task PreserveSpacing() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] + public async Task PreserveSpacing() + { + await TestAsync(""" + class C + { + string? M(string s) { - string? M(string s) - { - var l = s.ToLowerCase(); + var l = s.ToLowerCase(); - [||]if (l == "hello") - { - return null; - } + [||]if (l == "hello") + { + return null; + } - return l; + return l; - } } - """, - """ - class C + } + """, """ + class C + { + string? M(string s) { - string? M(string s) - { - var l = s.ToLowerCase(); + var l = s.ToLowerCase(); - if (l != "hello") - { - return l; - } + if (l != "hello") + { + return l; + } - return null; + return null; - } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] - public async Task PreserveSpacing_WithComments() - { - await TestInRegularAndScriptAsync( - """ - class C + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] + public async Task PreserveSpacing_WithComments() + { + await TestAsync(""" + class C + { + string? M(string s) { - string? M(string s) - { - var l = s.ToLowerCase(); + var l = s.ToLowerCase(); - [||]if (l == "hello") - { - // null 1 - return null; // null 2 - // null 3 - } + [||]if (l == "hello") + { + // null 1 + return null; // null 2 + // null 3 + } - // l 1 - return l; // l 2 - // l 3 + // l 1 + return l; // l 2 + // l 3 - } } - """, - """ - class C + } + """, """ + class C + { + string? M(string s) { - string? M(string s) - { - var l = s.ToLowerCase(); + var l = s.ToLowerCase(); - if (l != "hello") - { - // l 1 - return l; // l 2 - // null 3 - } + if (l != "hello") + { + // l 1 + return l; // l 2 + // null 3 + } - // null 1 - return null; // null 2 - // l 3 + // null 1 + return null; // null 2 + // l 3 - } } - """); - } + } + """); + } - [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] - public async Task PreserveSpacing_NoTrivia() - { - await TestInRegularAndScriptAsync( - """ - class C - { - string? M(bool b) - {[||]if(b){return(true);}return(false);} - } - """, - """ - class C - { - string? M(bool b) - { if (!b) { return (false); } return (true); } - } - """); - } + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/42715")] + public async Task PreserveSpacing_NoTrivia() + { + await TestAsync(""" + class C + { + string? M(bool b) + {[||]if(b){return(true);}return(false);} + } + """, """ + class C + { + string? M(bool b) + { if (!b) { return (false); } return (true); } + } + """); } } From 6449f1e1dc02f40fe2396d78b49f2b768b6a87c3 Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Sun, 12 May 2024 19:50:11 +0300 Subject: [PATCH 2/3] Port VB --- ...ertIfCodeRefactoringProvider.SingleLine.vb | 4 +- .../InvertIf/InvertSingleLineIfTests.vb | 169 +++++++++--------- 2 files changed, 83 insertions(+), 90 deletions(-) diff --git a/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.SingleLine.vb b/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.SingleLine.vb index 2c2325b37ca3a..a83e71dd19c96 100644 --- a/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.SingleLine.vb +++ b/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.SingleLine.vb @@ -3,8 +3,8 @@ ' See the LICENSE file in the project root for more information. Imports System.Composition -Imports System.Diagnostics.CodeAnalysis Imports Microsoft.CodeAnalysis.CodeRefactorings +Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -14,7 +14,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InvertIf Inherits VisualBasicInvertIfCodeRefactoringProvider(Of SingleLineIfStatementSyntax) - + Public Sub New() End Sub diff --git a/src/Features/VisualBasicTest/InvertIf/InvertSingleLineIfTests.vb b/src/Features/VisualBasicTest/InvertIf/InvertSingleLineIfTests.vb index cb49815aa7f34..41ad87bbb660e 100644 --- a/src/Features/VisualBasicTest/InvertIf/InvertSingleLineIfTests.vb +++ b/src/Features/VisualBasicTest/InvertIf/InvertSingleLineIfTests.vb @@ -2,22 +2,24 @@ ' The .NET Foundation licenses this file to you under the MIT license. ' See the LICENSE file in the project root for more information. -Imports Microsoft.CodeAnalysis.CodeRefactorings -Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces -Imports Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings -Imports Microsoft.CodeAnalysis.VisualBasic.InvertIf +Imports Microsoft.CodeAnalysis.Testing +Imports VerifyVB = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.VisualBasicCodeRefactoringVerifier(Of + Microsoft.CodeAnalysis.VisualBasic.InvertIf.VisualBasicInvertSingleLineIfCodeRefactoringProvider) Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.InvertIf - + Public Class InvertSingleLineIfTests - Inherits AbstractVisualBasicCodeActionTest_NoEditor - - Protected Overrides Function CreateCodeRefactoringProvider(workspace As TestWorkspace, parameters As TestParameters) As CodeRefactoringProvider - Return New VisualBasicInvertSingleLineIfCodeRefactoringProvider() + Private Shared Async Function TestInsideSubAsync(initial As String, expected As String) As Task + Await TestAsync(CreateTreeText(initial), CreateTreeText(expected)) End Function - Public Async Function TestFixOneAsync(initial As String, expected As String) As Task - Await TestInRegularAndScriptAsync(CreateTreeText(initial), CreateTreeText(expected)) + Private Shared Async Function TestAsync(initial As String, expected As String) As Task + Await New VerifyVB.Test With + { + .TestCode = initial, + .FixedCode = expected, + .CompilerDiagnostics = CompilerDiagnostics.None + }.RunAsync() End Function Public Shared Function CreateTreeText(initial As String) As String @@ -55,7 +57,7 @@ End Module Public Async Function TestAnd() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a And b Then aMethod() Else bMethod() ", @@ -67,7 +69,7 @@ End Module Public Async Function TestAddEmptyArgumentListIfNeeded() As Task Dim markup = - +" Module A Sub Main() [||]If True Then : Goo : Goo @@ -77,14 +79,14 @@ Module A Sub Goo() End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestAndAlso() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a AndAlso b Then aMethod() Else bMethod() ", @@ -95,7 +97,7 @@ End Module Public Async Function TestCall() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a.Goo() Then aMethod() Else bMethod() ", @@ -106,7 +108,7 @@ End Module Public Async Function TestNotIdentifier() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If Not a Then aMethod() Else bMethod() ", @@ -117,7 +119,7 @@ End Module Public Async Function TestTrueLiteral() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If True Then aMethod() Else bMethod() ", @@ -128,7 +130,7 @@ End Module Public Async Function TestFalseLiteral() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If False Then aMethod() Else bMethod() ", @@ -139,7 +141,7 @@ End Module Public Async Function TestEquals() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a = b Then aMethod() Else bMethod() ", @@ -150,7 +152,7 @@ End Module Public Async Function TestNotEquals() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a <> b Then aMethod() Else bMethod() ", @@ -161,7 +163,7 @@ End Module Public Async Function TestLessThan() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a < b Then aMethod() Else bMethod() ", @@ -172,7 +174,7 @@ End Module Public Async Function TestLessThanOrEqual() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a <= b Then aMethod() Else bMethod() ", @@ -183,7 +185,7 @@ End Module Public Async Function TestGreaterThan() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a > b Then aMethod() Else bMethod() ", @@ -194,7 +196,7 @@ End Module Public Async Function TestGreaterThanOrEqual() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a >= b Then aMethod() Else bMethod() ", @@ -205,7 +207,7 @@ End Module Public Async Function TestIs() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " Dim myObject As New Object Dim thisObject = myObject @@ -222,7 +224,7 @@ End Module Public Async Function TestIsNot() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " Dim myObject As New Object Dim thisObject = myObject @@ -239,7 +241,7 @@ End Module Public Async Function TestOr() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a Or b Then aMethod() Else bMethod() ", @@ -250,7 +252,7 @@ End Module Public Async Function TestOrElse() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a OrElse b Then aMethod() Else bMethod() ", @@ -261,7 +263,7 @@ End Module Public Async Function TestOr2() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " I[||]f Not a Or Not b Then aMethod() Else bMethod() ", @@ -272,7 +274,7 @@ End Module Public Async Function TestOrElse2() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " I[||]f Not a OrElse Not b Then aMethod() Else bMethod() ", @@ -283,7 +285,7 @@ End Module Public Async Function TestAnd2() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If Not a And Not b Then aMethod() Else bMethod() ", @@ -294,7 +296,7 @@ End Module Public Async Function TestAndAlso2() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If Not a AndAlso Not b Then aMethod() Else bMethod() ", @@ -305,7 +307,7 @@ End Module Public Async Function TestXor() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " I[||]f a Xor b Then aMethod() Else bMethod() ", @@ -317,7 +319,7 @@ End Module Public Async Function TestXor2() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " I[||]f Not (a Xor b) Then aMethod() Else bMethod() ", @@ -328,7 +330,7 @@ End Module Public Async Function TestNested() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If (((a = b) AndAlso (c <> d)) OrElse ((e < f) AndAlso (Not g))) Then aMethod() Else bMethod() ", @@ -340,7 +342,7 @@ End Module Public Async Function TestEscapeKeywordsIfNeeded1() As Task Dim markup = - +" Imports System.Linq Module Program Sub Main() @@ -350,15 +352,15 @@ Module Program Sub Take() End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestEscapeKeywordsIfNeeded2() As Task Dim markup = - +" Imports System.Linq Module Program Sub Main() @@ -368,15 +370,15 @@ Module Program Sub Ascending() End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestEscapeKeywordsIfNeeded3() As Task Dim markup = - +" Imports System.Linq Module Program Sub Main() @@ -386,15 +388,15 @@ Module Program Sub Ascending() End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestEscapeKeywordsIfNeeded4() As Task Dim markup = - +" Imports System.Linq Module Program Sub Main() @@ -402,26 +404,15 @@ Module Program Take: Return End Sub End Module - - - Dim expected = - -Imports System.Linq -Module Program - Sub Main() - If False Then Console.WriteLine() Else Dim q = From x In "" -[Take]: Return - End Sub -End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestEscapeKeywordsIfNeeded5() As Task Dim markup = - +" Imports System.Linq Module Program Sub Main() @@ -431,14 +422,14 @@ Module Program Sub Take() End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestSelection() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [|If a And b Then aMethod() Else bMethod()|] ", @@ -449,7 +440,7 @@ End Module Public Async Function TestMultipleStatementsSingleLineIfStatement() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " If[||] a Then aMethod() : bMethod() Else cMethod() : d() ", @@ -460,7 +451,7 @@ End Module Public Async Function TestTriviaAfterSingleLineIfStatement() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a Then aMethod() Else bMethod() ' I will stay put ", @@ -470,7 +461,7 @@ End Module End Function Public Async Function TestParenthesizeForLogicalExpressionPrecedence() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Sub Main() I[||]f a AndAlso b Or c Then aMethod() Else bMethod() End Sub @@ -483,7 +474,7 @@ End Module") Public Async Function TestParenthesizeComparisonOperands() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If 0 <= .GetHashCode Then aMethod() Else bMethod() ", @@ -496,7 +487,7 @@ End Module") Public Async Function TestNestedSingleLineIfs() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() ' Invert the 1st If @@ -513,18 +504,20 @@ End Module") Public Async Function TestTryToParenthesizeAwkwardSyntaxInsideSingleLineLambdaMethod() As Task - Await TestMissingAsync( + Dim markup = "Module Program Sub Main() ' Invert If Dim x = Sub() I[||]f True Then Dim y Else Console.WriteLine(), z = 1 End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestOnConditionOfSingleLineIf() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) If T[||]rue Then Return Else Console.WriteLine(""a"") @@ -541,42 +534,42 @@ End Module") Public Async Function TestImplicitLineContinuationBeforeClosingParenIsRemoved() As Task Dim markup = - +" [||]If (True OrElse True ) Then Else End If - +" Dim expected = - +" If False AndAlso False Then Else End If - +" - Await TestAsync(markup, expected) + Await TestInsideSubAsync(markup, expected) End Function Public Async Function TestParenthesizeToKeepParseTheSame1() As Task Dim markup = - +" Module Program Sub Main() - [||]If 0 >= <x/>.GetHashCode Then Console.WriteLine(1) Else Console.WriteLine(2) + [||]If 0 >= .GetHashCode Then Console.WriteLine(1) Else Console.WriteLine(2) End Sub End Module - +" Dim expected = - +" Module Program Sub Main() - If 0 < (<x/>.GetHashCode) Then Console.WriteLine(2) Else Console.WriteLine(1) + If 0 < (.GetHashCode) Then Console.WriteLine(2) Else Console.WriteLine(1) End Sub End Module - +" Await TestAsync(markup, expected) End Function @@ -584,7 +577,7 @@ End Module Public Async Function TestParenthesizeToKeepParseTheSame2() As Task Dim markup = - +" Module Program Sub Main() Select Nothing @@ -592,14 +585,14 @@ Module Program End Select End Sub End Module - +" - Await TestMissingAsync(markup) + Await TestAsync(markup, markup) End Function Public Async Function TestSingleLineIdentifier() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a Then aMethod() Else bMethod() ", @@ -610,7 +603,7 @@ End Module Public Async Function TestWithMissingTrueStatementWithinUsing() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub M(Disposable As IDisposable) Dim x = True From 99f2c59b5b50387eb3c628cd772ad3c52a65ec50 Mon Sep 17 00:00:00 2001 From: DoctorKrolic Date: Sun, 12 May 2024 20:20:07 +0300 Subject: [PATCH 3/3] Port VB --- ...vertIfCodeRefactoringProvider.MultiLine.vb | 4 +- .../InvertIf/InvertMultiLineIfTests.vb | 151 ++++++++++-------- 2 files changed, 89 insertions(+), 66 deletions(-) diff --git a/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.MultiLine.vb b/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.MultiLine.vb index 2cadad82d13ef..b91b722459024 100644 --- a/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.MultiLine.vb +++ b/src/Features/VisualBasic/Portable/InvertIf/VisualBasicInvertIfCodeRefactoringProvider.MultiLine.vb @@ -3,8 +3,8 @@ ' See the LICENSE file in the project root for more information. Imports System.Composition -Imports System.Diagnostics.CodeAnalysis Imports Microsoft.CodeAnalysis.CodeRefactorings +Imports Microsoft.CodeAnalysis.Host.Mef Imports Microsoft.CodeAnalysis.Text Imports Microsoft.CodeAnalysis.VisualBasic.Syntax @@ -14,7 +14,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.InvertIf Inherits VisualBasicInvertIfCodeRefactoringProvider(Of MultiLineIfBlockSyntax) - + Public Sub New() End Sub diff --git a/src/Features/VisualBasicTest/InvertIf/InvertMultiLineIfTests.vb b/src/Features/VisualBasicTest/InvertIf/InvertMultiLineIfTests.vb index 03cadc892d57f..de65c9f26ff25 100644 --- a/src/Features/VisualBasicTest/InvertIf/InvertMultiLineIfTests.vb +++ b/src/Features/VisualBasicTest/InvertIf/InvertMultiLineIfTests.vb @@ -2,22 +2,25 @@ ' The .NET Foundation licenses this file to you under the MIT license. ' See the LICENSE file in the project root for more information. -Imports Microsoft.CodeAnalysis.CodeRefactorings -Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces -Imports Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings -Imports Microsoft.CodeAnalysis.VisualBasic.InvertIf +Imports Microsoft.CodeAnalysis.Testing +Imports VerifyVB = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.VisualBasicCodeRefactoringVerifier(Of + Microsoft.CodeAnalysis.VisualBasic.InvertIf.VisualBasicInvertMultiLineIfCodeRefactoringProvider) Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.InvertIf - + Public Class InvertMultiLineIfTests - Inherits AbstractVisualBasicCodeActionTest_NoEditor - - Protected Overrides Function CreateCodeRefactoringProvider(workspace As TestWorkspace, parameters As TestParameters) As CodeRefactoringProvider - Return New VisualBasicInvertMultiLineIfCodeRefactoringProvider() + Private Shared Async Function TestInsideSubAsync(initial As String, expected As String, Optional languageVersion As LanguageVersion = LanguageVersion.Latest) As Task + Await TestAsync(CreateTreeText(initial), CreateTreeText(expected), languageVersion) End Function - Public Async Function TestFixOneAsync(initial As String, expected As String, Optional parseOptions As ParseOptions = Nothing) As Task - Await TestInRegularAndScriptAsync(CreateTreeText(initial), CreateTreeText(expected), parseOptions:=parseOptions) + Private Shared Async Function TestAsync(initial As String, expected As String, Optional languageVersion As LanguageVersion = LanguageVersion.Latest) As Task + Await New VerifyVB.Test With + { + .TestCode = initial, + .FixedCode = expected, + .LanguageVersion = languageVersion, + .CompilerDiagnostics = CompilerDiagnostics.None + }.RunAsync() End Function Public Shared Function CreateTreeText(initial As String) As String @@ -55,7 +58,7 @@ End Module Public Async Function TestMultiLineIdentifier() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If a Then aMethod() @@ -74,7 +77,7 @@ End Module Public Async Function TestElseIf() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = " Sub Main() If a Then @@ -85,12 +88,14 @@ Sub Main() cMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestKeepElseIfKeyword() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() If a Then @@ -101,12 +106,14 @@ End Module") cMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnIfElseIfElse() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() I[||]f a Then @@ -117,12 +124,14 @@ End Module") cMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestSelection() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [|If a Then aMethod() @@ -141,7 +150,7 @@ End Module") Public Async Function TestDoesNotOverlapHiddenPosition1() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() #End ExternalSource @@ -170,7 +179,7 @@ End Module") Public Async Function TestDoesNotOverlapHiddenPosition2() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() #ExternalSource File.vb 1 @@ -197,7 +206,7 @@ End Module") Public Async Function TestMissingOnOverlapsHiddenPosition1() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() [||]If a Then @@ -208,12 +217,14 @@ End Module") bMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnOverlapsHiddenPosition2() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() If a Then @@ -226,12 +237,14 @@ End Module") cMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnOverlapsHiddenPosition3() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() [||]If a Then @@ -244,12 +257,14 @@ End Module") cMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnOverlapsHiddenPosition4() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() [||]If a Then @@ -260,12 +275,14 @@ End Module") #End ExternalSource End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnOverlapsHiddenPosition5() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() [||]If a Then @@ -276,12 +293,14 @@ End Module") #End ExternalSource End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMissingOnOverlapsHiddenPosition6() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main() [||]If a Then @@ -292,12 +311,14 @@ End Module") bMethod() End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestMultipleStatementsMultiLineIfBlock() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() [||]If a Then @@ -324,7 +345,7 @@ End Module") Public Async Function TestTriviaAfterMultiLineIfBlock() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() [||]If a Then @@ -347,7 +368,7 @@ End Module") Public Async Function TestKeepExplicitLineContinuationTriviaMethod() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() I[||]f a And b _ @@ -372,7 +393,7 @@ End Module") Public Async Function TestKeepTriviaInStatementsInMultiLineIfBlock() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() [||]If a Then @@ -400,7 +421,7 @@ End Module") Public Async Function TestSimplifyToLengthEqualsZero() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x As String @@ -425,7 +446,7 @@ End Module") Public Async Function TestSimplifyToLengthEqualsZero2() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x() As String @@ -450,7 +471,7 @@ End Module") Public Async Function TestSimplifyToLengthEqualsZero4() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x() As String @@ -475,7 +496,7 @@ End Module") Public Async Function TestSimplifyToLengthEqualsZero5() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x As String @@ -500,7 +521,7 @@ End Module") Public Async Function TestDoesNotSimplifyToLengthEqualsZero() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x As String @@ -525,7 +546,7 @@ End Module") Public Async Function TestDoesNotSimplifyToLengthEqualsZero2() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) Dim x As String @@ -552,7 +573,7 @@ End Module") Public Async Function TestColonAfterSingleLineIfWithEmptyElse() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main() ' Invert If @@ -570,7 +591,7 @@ End Module") Public Async Function TestOnlyOnElseIf() As Task - Await TestMissingInRegularAndScriptAsync( + Dim markup = "Module Program Sub Main(args As String()) If False Then @@ -581,12 +602,14 @@ End Module") Console.WriteLine(""a"") End If End Sub -End Module") +End Module" + + Await TestAsync(markup, markup) End Function Public Async Function TestOnConditionOfMultiLineIfStatement() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "Module Program Sub Main(args As String()) If [||]False Then @@ -611,12 +634,12 @@ End Module") Public Async Function TestDoNotRemoveTypeCharactersDuringComplexification() As Task Dim markup = - +" Imports System Module Program Sub Main() Goo(Function(take) - [||]If True Then Console.WriteLine("true") Else Console.WriteLine("false") + [||]If True Then Console.WriteLine(""true"") Else Console.WriteLine(""false"") take$.ToString() Return Function() 1 End Function) @@ -626,15 +649,15 @@ Imports System Sub Goo(Of T)(x As Func(Of Integer, T)) End Sub End Module - +" Dim expected = - +" Imports System Module Program Sub Main() Goo(Function(take) - If False Then Console.WriteLine("false") Else Console.WriteLine("true") + If False Then Console.WriteLine(""false"") Else Console.WriteLine(""true"") take$.ToString() Return Function() 1 End Function) @@ -644,14 +667,14 @@ Imports System Sub Goo(Of T)(x As Func(Of Integer, T)) End Sub End Module - +" Await TestAsync(markup, expected) End Function Public Async Function InvertIfWithoutStatements() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "class C sub M(x as String) [||]If x = ""a"" Then @@ -677,7 +700,7 @@ end class") Public Async Function InvertIfWithOnlyComment() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "class C sub M(x as String) [||]If x = ""a"" Then @@ -706,7 +729,7 @@ end class") Public Async Function InvertIfWithoutElse() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( "class C sub M(x as String) [||]If x = ""a"" Then @@ -730,7 +753,7 @@ end class") Public Async Function TestMultiLineTypeOfIs_VB12() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If TypeOf a Is String Then aMethod() @@ -744,12 +767,12 @@ end class") Else aMethod() End If -", VisualBasicParseOptions.Default.WithLanguageVersion(LanguageVersion.VisualBasic12)) +", LanguageVersion.VisualBasic12) End Function Public Async Function TestMultiLineTypeOfIs_VB14() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If TypeOf a Is String Then aMethod() @@ -763,12 +786,12 @@ end class") Else aMethod() End If -", VisualBasicParseOptions.Default.WithLanguageVersion(LanguageVersion.VisualBasic14)) +", LanguageVersion.VisualBasic14) End Function Public Async Function TestMultiLineTypeOfIsNot() As Task - Await TestFixOneAsync( + Await TestInsideSubAsync( " [||]If TypeOf a IsNot String Then aMethod() @@ -788,7 +811,7 @@ end class") Public Async Function PreserveSpace() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( " class C sub M(s as string) @@ -819,7 +842,7 @@ end class") Public Async Function PreserveSpace_WithComments() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( " class C sub M(s as string) @@ -858,7 +881,7 @@ end class") Public Async Function PreserveSpace_NoTrivia() As Task - Await TestInRegularAndScriptAsync( + Await TestAsync( " class C sub M(s as string)