未验证 提交 8a7bfd86 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #45608 from sharwell/make-synchronous

Convert MakeMethodSynchronousTests to the new test library
......@@ -3,30 +3,28 @@
// See the LICENSE file in the project root for more information.
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.MakeMethodSynchronous;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Testing;
using Roslyn.Test.Utilities;
using Xunit;
using VerifyCS = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.CSharpCodeFixVerifier<
Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer,
Microsoft.CodeAnalysis.CSharp.MakeMethodSynchronous.CSharpMakeMethodSynchronousCodeFixProvider>;
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.Diagnostics.MakeMethodSynchronous
{
public class MakeMethodSynchronousTests : AbstractCSharpDiagnosticProviderBasedUserDiagnosticTest
public class MakeMethodSynchronousTests
{
internal override (DiagnosticAnalyzer, CodeFixProvider) CreateDiagnosticProviderAndFixer(Workspace workspace)
=> (null, new CSharpMakeMethodSynchronousCodeFixProvider());
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestTaskReturnType()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async Task [|Goo|]()
async Task {|CS1998:Goo|}()
{
}
}",
......@@ -44,14 +42,15 @@ void Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestTaskOfTReturnType()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async Task<int> [|Goo|]()
async Task<int> {|CS1998:Goo|}()
{
return 1;
}
}",
@"
......@@ -59,8 +58,9 @@ class C
class C
{
int Goo()
int {|#0:Goo|}()
{
return 1;
}
}");
}
......@@ -68,13 +68,13 @@ int Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestSecondModifier()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
public async Task [|Goo|]()
public async Task {|CS1998:Goo|}()
{
}
}",
......@@ -92,13 +92,13 @@ public void Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestFirstModifier()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async public Task [|Goo|]()
async public Task {|CS1998:Goo|}()
{
}
}",
......@@ -116,14 +116,14 @@ public void Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestTrailingTrivia()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async // comment
Task [|Goo|]()
Task {|CS1998:Goo|}()
{
}
}",
......@@ -141,13 +141,13 @@ void Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestRenameMethod()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
}",
......@@ -165,13 +165,13 @@ void Goo()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestRenameMethod1()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"
using System.Threading.Tasks;
class C
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
......@@ -199,8 +199,9 @@ void Bar()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestParenthesizedLambda()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -208,10 +209,12 @@ class C
void Goo()
{
Func<Task> f =
async () [|=>|] { };
async () {|CS1998:=>|} { };
}
}",
}";
var expected =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -219,16 +222,31 @@ class C
void Goo()
{
Func<Task> f =
() => { };
() {|#0:=>|} { };
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
ExpectedDiagnostics =
{
// /0/Test0.cs(10,16): error CS1643: Not all code paths return a value in lambda expression of type 'Func<Task>'
DiagnosticResult.CompilerError("CS1643").WithLocation(0),
},
},
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestSimpleLambda()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -236,10 +254,12 @@ class C
void Goo()
{
Func<string, Task> f =
async a [|=>|] { };
async a {|CS1998:=>|} { };
}
}",
}";
var expected =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -247,44 +267,78 @@ class C
void Goo()
{
Func<string, Task> f =
a => { };
a {|#0:=>|} { };
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
ExpectedDiagnostics =
{
// /0/Test0.cs(10,15): error CS1643: Not all code paths return a value in lambda expression of type 'Func<Task>'
DiagnosticResult.CompilerError("CS1643").WithLocation(0),
},
},
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestLambdaWithExpressionBody()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System;
using System.Threading.Tasks;
class C
{
void Goo()
{
Func<string, Task> f =
async a [|=>|] 1;
Func<string, Task<int>> f =
async a {|CS1998:=>|} 1;
}
}",
}";
var expected =
@"
using System;
using System.Threading.Tasks;
class C
{
void Goo()
{
Func<string, Task> f =
a => 1;
Func<string, Task<int>> f =
a => {|#0:1|};
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
ExpectedDiagnostics =
{
// /0/Test0.cs(10,18): error CS0029: Cannot implicitly convert type 'int' to 'System.Threading.Tasks.Task<int>'
DiagnosticResult.CompilerError("CS0029").WithLocation(0).WithArguments("int", "System.Threading.Tasks.Task<int>"),
// /0/Test0.cs(10,18): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
DiagnosticResult.CompilerError("CS1662").WithLocation(0),
},
},
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestAnonymousMethod()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -292,10 +346,12 @@ class C
void Goo()
{
Func<Task> f =
async [|delegate|] { };
async {|CS1998:delegate|} { };
}
}",
}";
var expected =
@"
using System;
using System.Threading.Tasks;
class C
......@@ -303,28 +359,43 @@ class C
void Goo()
{
Func<Task> f =
delegate { };
{|#0:delegate|} { };
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
ExpectedDiagnostics =
{
// /0/Test0.cs(10,13): error CS1643: Not all code paths return a value in anonymous method of type 'Func<Task>'
DiagnosticResult.CompilerError("CS1643").WithLocation(0),
},
},
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestFixAll()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"using System.Threading.Tasks;
public class Class1
{
{|FixAllInDocument:async Task GooAsync()
async Task {|CS1998:GooAsync|}()
{
BarAsync();
}
async Task<int> BarAsync()
async Task<int> {|#0:{|CS1998:BarAsync|}|}()
{
GooAsync();
}|}
return 1;
}
}",
@"using System.Threading.Tasks;
......@@ -335,9 +406,10 @@ void Goo()
Bar();
}
int Bar()
int {|#0:Bar|}()
{
Goo();
return 1;
}
}");
}
......@@ -346,12 +418,12 @@ int Bar()
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCaller1()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
......@@ -359,7 +431,8 @@ async void BarAsync()
{
await GooAsync();
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
......@@ -368,23 +441,34 @@ void Goo()
{
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
Goo();
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCaller2()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
......@@ -392,7 +476,8 @@ async void BarAsync()
{
await GooAsync().ConfigureAwait(false);
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
......@@ -401,23 +486,34 @@ void Goo()
{
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
Goo();
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCaller3()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
......@@ -425,7 +521,8 @@ async void BarAsync()
{
await this.GooAsync();
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
......@@ -434,23 +531,34 @@ void Goo()
{
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
this.Goo();
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCaller4()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task [|GooAsync|]()
async Task {|CS1998:GooAsync|}()
{
}
......@@ -458,7 +566,8 @@ async void BarAsync()
{
await this.GooAsync().ConfigureAwait(false);
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
......@@ -467,91 +576,130 @@ void Goo()
{
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
this.Goo();
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCallerNested1()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task<int> [|GooAsync|](int i)
async Task<int> {|CS1998:GooAsync|}(int i)
{
return 1;
}
async void BarAsync()
{
await this.GooAsync(await this.GooAsync(0));
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
{
int Goo(int i)
{
return 1;
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
this.Goo(this.Goo(0));
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
[WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")]
public async Task TestRemoveAwaitFromCallerNested()
{
await TestInRegularAndScriptAsync(
var source =
@"using System.Threading.Tasks;
public class Class1
{
async Task<int> [|GooAsync|](int i)
async Task<int> {|CS1998:GooAsync|}(int i)
{
return 1;
}
async void BarAsync()
{
await this.GooAsync(await this.GooAsync(0).ConfigureAwait(false)).ConfigureAwait(false);
}
}",
}";
var expected =
@"using System.Threading.Tasks;
public class Class1
{
int Goo(int i)
{
return 1;
}
async void BarAsync()
async void {|CS1998:BarAsync|}()
{
this.Goo(this.Goo(0));
}
}");
}";
await new VerifyCS.Test
{
TestCode = source,
FixedState =
{
Sources = { expected },
MarkupHandling = MarkupMode.Allow,
},
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodAsynchronous)]
[WorkItem(14133, "https://github.com/dotnet/roslyn/issues/14133")]
public async Task RemoveAsyncInLocalFunction()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"using System.Threading.Tasks;
class C
{
public void M1()
{
async Task [|M2Async|]()
async Task {|CS1998:M2Async|}()
{
}
}
......@@ -578,7 +726,7 @@ void M2()
[WorkItem(18307, "https://github.com/dotnet/roslyn/issues/18307")]
public async Task RemoveAsyncInLocalFunctionKeepsTrivia(string asyncReturn, string expectedReturn)
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Threading.Tasks;
......@@ -587,7 +735,7 @@ class C
public void M1()
{{
// Leading trivia
/*1*/ async {asyncReturn} /*2*/ [|M2Async|]/*3*/() /*4*/
/*1*/ async {asyncReturn} /*2*/ {{|CS1998:M2Async|}}/*3*/() /*4*/
{{
throw new NotImplementedException();
}}
......@@ -623,14 +771,14 @@ public void M1()
[WorkItem(18307, "https://github.com/dotnet/roslyn/issues/18307")]
public async Task RemoveAsyncKeepsTrivia(string modifiers, string asyncReturn, string expectedReturn)
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
$@"using System;
using System.Threading.Tasks;
class C
{{
// Leading trivia
{modifiers}/*1*/ async {asyncReturn} /*2*/ [|M2Async|]/*3*/() /*4*/
{modifiers}/*1*/ async {asyncReturn} /*2*/ {{|CS1998:M2Async|}}/*3*/() /*4*/
{{
throw new NotImplementedException();
}}
......@@ -651,36 +799,50 @@ class C
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithUsingAwait()
{
await TestMissingInRegularAndScriptAsync(
var source =
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task MAsync()
{
await using (var x = new object())
await using ({|#0:var x = new object()|})
{
}
}
}");
}";
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard21,
TestCode = source,
ExpectedDiagnostics =
{
// /0/Test0.cs(5,22): error CS8410: 'object': type used in an asynchronous using statement must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method.
DiagnosticResult.CompilerError("CS8410").WithLocation(0).WithArguments("object"),
},
FixedCode = source,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithUsingNoAwait()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task {|CS1998:MAsync|}()
{
using (var x = new object())
using ({|#0:var x = new object()|})
{
}
}
}",
// /0/Test0.cs(5,16): error CS1674: 'object': type used in a using statement must be implicitly convertible to 'System.IDisposable'.
DiagnosticResult.CompilerError("CS1674").WithLocation(0).WithArguments("object"),
@"class C
{
void [|M|]()
void M()
{
using (var x = new object())
using ({|#0:var x = new object()|})
{
}
}
......@@ -690,25 +852,31 @@ public async Task MethodWithUsingNoAwait()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithAwaitForEach()
{
await TestMissingInRegularAndScriptAsync(
var source =
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task MAsync()
{
await foreach (var n in new int[] { })
await foreach (var n in {|#0:new int[] { }|})
{
}
}
}");
}";
await VerifyCS.VerifyCodeFixAsync(
source,
// /0/Test0.cs(5,33): error CS1061: 'bool' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)
DiagnosticResult.CompilerError("CS1061").WithLocation(0).WithArguments("bool", "GetAwaiter"),
source);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithForEachNoAwait()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task {|CS1998:MAsync|}()
{
foreach (var n in new int[] { })
{
......@@ -717,7 +885,7 @@ public async Task MethodWithForEachNoAwait()
}",
@"class C
{
void [|M|]()
void M()
{
foreach (var n in new int[] { })
{
......@@ -729,25 +897,31 @@ public async Task MethodWithForEachNoAwait()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithForEachVariableAwait()
{
await TestMissingInRegularAndScriptAsync(
var source =
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task MAsync()
{
await foreach (var (a, b) in new(int, int)[] { })
await foreach (var (a, b) in {|#0:new(int, int)[] { }|})
{
}
}
}");
}";
await VerifyCS.VerifyCodeFixAsync(
source,
// /0/Test0.cs(5,38): error CS1061: 'bool' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'bool' could be found (are you missing a using directive or an assembly reference?)
DiagnosticResult.CompilerError("CS1061").WithLocation(0).WithArguments("bool", "GetAwaiter"),
source);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task MethodWithForEachVariableNoAwait()
{
await TestInRegularAndScriptAsync(
await VerifyCS.VerifyCodeFixAsync(
@"class C
{
async System.Threading.Tasks.Task [|MAsync|]()
async System.Threading.Tasks.Task {|CS1998:MAsync|}()
{
foreach (var (a, b) in new(int, int)[] { })
{
......@@ -756,7 +930,7 @@ public async Task MethodWithForEachVariableNoAwait()
}",
@"class C
{
void [|M|]()
void M()
{
foreach (var (a, b) in new (int, int)[] { })
{
......@@ -768,18 +942,19 @@ public async Task MethodWithForEachVariableNoAwait()
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestIAsyncEnumerableReturnType()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System.Threading.Tasks;
using System.Collections.Generic;
class C
{
async IAsyncEnumerable<int> [|MAsync|]()
async IAsyncEnumerable<int> {|CS1998:MAsync|}()
{
yield return 1;
}
}" + IAsyncEnumerable,
}";
var expected =
@"
using System.Threading.Tasks;
using System.Collections.Generic;
......@@ -790,13 +965,20 @@ IEnumerable<int> M()
{
yield return 1;
}
}" + IAsyncEnumerable);
}";
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard21,
TestCode = source,
FixedCode = expected,
}.RunAsync();
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)]
public async Task TestIAsyncEnumeratorReturnTypeOnLocalFunction()
{
await TestInRegularAndScriptAsync(
var source =
@"
using System.Threading.Tasks;
using System.Collections.Generic;
......@@ -805,12 +987,13 @@ class C
{
void Method()
{
async IAsyncEnumerator<int> [|MAsync|]()
async IAsyncEnumerator<int> {|CS1998:MAsync|}()
{
yield return 1;
}
}
}" + IAsyncEnumerable,
}";
var expected =
@"
using System.Threading.Tasks;
using System.Collections.Generic;
......@@ -824,7 +1007,14 @@ IEnumerator<int> M()
yield return 1;
}
}
}" + IAsyncEnumerable);
}";
await new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard21,
TestCode = source,
FixedCode = expected,
}.RunAsync();
}
}
}
......@@ -2,25 +2,21 @@
' 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.CodeFixes
Imports Microsoft.CodeAnalysis.Diagnostics
Imports Microsoft.CodeAnalysis.VisualBasic.MakeMethodSynchronous
Imports Microsoft.CodeAnalysis.Testing
Imports VerifyVB = Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions.VisualBasicCodeFixVerifier(
Of Microsoft.CodeAnalysis.Testing.EmptyDiagnosticAnalyzer,
Microsoft.CodeAnalysis.VisualBasic.MakeMethodSynchronous.VisualBasicMakeMethodSynchronousCodeFixProvider)
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.Diagnostics.MakeMethodSynchronous
Public Class MakeMethodSynchronousTests
Inherits AbstractVisualBasicDiagnosticProviderBasedUserDiagnosticTest
Friend Overrides Function CreateDiagnosticProviderAndFixer(workspace As Workspace) As (DiagnosticAnalyzer, CodeFixProvider)
Return (Nothing, New VisualBasicMakeMethodSynchronousCodeFixProvider())
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestTaskReturnType() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Async Function [|Goo|]() As Task
Async Function {|BC42356:Goo|}() As Task
End Function
End Class",
"Imports System.Threading.Tasks
......@@ -33,11 +29,11 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestTaskOfTReturnType() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Async Function [|Goo|]() As Task(of String)
Async Function {|BC42356:Goo|}() As Task(of String)
End Function
End Class",
"Imports System.Threading.Tasks
......@@ -50,11 +46,11 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestSecondModifier() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Public Async Function [|Goo|]() As Task
Public Async Function {|BC42356:Goo|}() As Task
End Function
End Class",
"Imports System.Threading.Tasks
......@@ -67,11 +63,11 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestFirstModifier() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Async Public Function [|Goo|]() As Task
Async Public Function {|BC42356:Goo|}() As Task
End Function
End Class",
"Imports System.Threading.Tasks
......@@ -84,11 +80,11 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestRenameMethod() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Async Sub [|GooAsync|]()
Async Sub {|BC42356:GooAsync|}()
End Sub
End Class",
"Imports System.Threading.Tasks
......@@ -101,11 +97,11 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestRenameMethod1() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System.Threading.Tasks
Class C
Async Sub [|GooAsync|]()
Async Sub {|BC42356:GooAsync|}()
End Sub
Sub Bar()
......@@ -126,14 +122,14 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestSingleLineSubLambda() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System
Imports System.Threading.Tasks
Class C
Sub Goo()
dim f as Action(of Task) =
Async [|Sub|]() Return
Async {|BC42356:Sub|}() Return
End Sub
End Class",
"Imports System
......@@ -149,37 +145,45 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestSingleLineFunctionLambda() As Task
Await TestInRegularAndScriptAsync(
Dim source =
"Imports System
Imports System.Threading.Tasks
Class C
Sub Goo()
dim f as Func(of Task) =
Async [|Function|]() 1
Async {|BC42356:Function|}() 1
End Sub
End Class",
End Class"
Dim expected =
"Imports System
Imports System.Threading.Tasks
Class C
Sub Goo()
dim f as Func(of Task) =
Function() 1
Function() {|#0:1|}
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
' /0/Test0.vb(7) : error BC30311: Value of type 'Integer' cannot be converted to 'Task'.
test.FixedState.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError("BC30311").WithLocation(0).WithArguments("Integer", "System.Threading.Tasks.Task"))
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestMultiLineSubLambda() As Task
Await TestInRegularAndScriptAsync(
Await VerifyVB.VerifyCodeFixAsync(
"Imports System
Imports System.Threading.Tasks
Class C
Sub Goo()
dim f as Action(of Task) =
Async [|Sub|]()
Async {|BC42356:Sub|}()
Return
End Sub
End Sub
......@@ -199,18 +203,19 @@ End Class")
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
Public Async Function TestMultiLineFunctionLambda() As Task
Await TestInRegularAndScriptAsync(
Dim source =
"Imports System
Imports System.Threading.Tasks
Class C
Sub Goo()
dim f as Func(of Task) =
Async [|Function|]()
Async {|BC42356:Function|}()
Return 1
End Function
End Sub
End Class",
End Class"
Dim expected =
"Imports System
Imports System.Threading.Tasks
......@@ -218,166 +223,221 @@ Class C
Sub Goo()
dim f as Func(of Task) =
Function()
Return 1
Return {|#0:1|}
End Function
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
' /0/Test0.vb(8) : error BC30311: Value of type 'Integer' cannot be converted to 'Task'.
test.FixedState.ExpectedDiagnostics.Add(DiagnosticResult.CompilerError("BC30311").WithLocation(0).WithArguments("Integer", "System.Threading.Tasks.Task"))
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCaller1() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|]() As Task
Async Function {|BC42356:GooAsync|}() As Task
End Function
Async Sub BarAsync()
Await GooAsync()
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Sub Goo()
End Sub
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Goo()
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCaller2() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|]() As Task
Async Function {|BC42356:GooAsync|}() As Task
End Function
Async Sub BarAsync()
Await GooAsync().ConfigureAwait(false)
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Sub Goo()
End Sub
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Goo()
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCaller3() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|]() As Task
Async Function {|BC42356:GooAsync|}() As Task
End Function
Async Sub BarAsync()
Await Me.GooAsync()
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Sub Goo()
End Sub
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Me.Goo()
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCaller4() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|]() As Task
Async Function {|BC42356:GooAsync|}() As Task
End Function
Async Sub BarAsync()
Await Me.GooAsync().ConfigureAwait(false)
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Sub Goo()
End Sub
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Me.Goo()
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCallerNested1() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|](i As Integer) As Task(Of Integer)
Async Function {|BC42356:GooAsync|}(i As Integer) As Task(Of Integer)
End Function
Async Sub BarAsync()
Await GooAsync(Await GooAsync(0))
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Function Goo(i As Integer) As Integer
End Function
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Goo(Goo(0))
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
<Fact, Trait(Traits.Feature, Traits.Features.CodeActionsMakeMethodSynchronous)>
<WorkItem(13961, "https://github.com/dotnet/roslyn/issues/13961")>
Public Async Function TestRemoveAwaitFromCallerNested2() As Task
Await TestInRegularAndScriptAsync(
"Imports System.Threading.Tasks;
Dim source =
"Imports System.Threading.Tasks
Public Class Class1
Async Function [|GooAsync|](i As Integer) As Task(Of Integer)
Async Function {|BC42356:GooAsync|}(i As Integer) As Task(Of Integer)
End Function
Async Sub BarAsync()
Await Me.GooAsync(Await Me.GooAsync(0).ConfigureAwait(false)).ConfigureAwait(false)
End Sub
End Class",
"Imports System.Threading.Tasks;
End Class"
Dim expected =
"Imports System.Threading.Tasks
Public Class Class1
Function Goo(i As Integer) As Integer
End Function
Async Sub BarAsync()
Async Sub {|BC42356:BarAsync|}()
Me.Goo(Me.Goo(0))
End Sub
End Class")
End Class"
Dim test = New VerifyVB.Test()
test.TestState.Sources.Add(source)
test.FixedState.Sources.Add(expected)
test.FixedState.MarkupHandling = MarkupMode.Allow
test.CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne
Await test.RunAsync()
End Function
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册