forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create AddAwait refactoring (dotnet#28930)
- Loading branch information
Showing
40 changed files
with
875 additions
and
0 deletions.
There are no files selected for viewing
227 changes: 227 additions & 0 deletions
227
src/EditorFeatures/CSharpTest/CodeActions/AddAwait/AddAwaitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
using Microsoft.CodeAnalysis.CSharp.CodeRefactorings.AddAwait; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.CodeRefactorings.AddAwait | ||
{ | ||
[Trait(Traits.Feature, Traits.Features.AddAwait)] | ||
public class AddAwaitTests : AbstractCSharpCodeActionTest | ||
{ | ||
protected override CodeRefactoringProvider CreateCodeRefactoringProvider(Workspace workspace, TestParameters parameters) | ||
=> new CSharpAddAwaitCodeRefactoringProvider(); | ||
|
||
[Fact] | ||
public async Task Simple() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = GetNumberAsync()[||]; | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = await GetNumberAsync(); | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleWithConfigureAwait() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = GetNumberAsync()[||]; | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = await GetNumberAsync().ConfigureAwait(false); | ||
} | ||
}", index: 1); | ||
} | ||
|
||
[Fact] | ||
public async Task InArgument() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync(int argument) | ||
{ | ||
var x = GetNumberAsync(arg[||]ument); | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync(int argument) | ||
{ | ||
var x = await GetNumberAsync(argument); | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task AlreadyAwaited() | ||
{ | ||
await TestMissingInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = await GetNumberAsync()[||]; | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleWithTrivia() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = // comment | ||
GetNumberAsync()[||] /* comment */ | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = // comment | ||
await GetNumberAsync()[||] /* comment */ | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleWithTrivia2() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = /* comment */ GetNumberAsync()[||] // comment | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = /* comment */ await GetNumberAsync()[||] // comment | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleWithTriviaWithConfigureAwait() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = // comment | ||
GetNumberAsync()[||] /* comment */ | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = // comment | ||
await GetNumberAsync().ConfigureAwait(false) /* comment */ | ||
} | ||
}", index: 1); | ||
} | ||
|
||
[Fact] | ||
public async Task SimpleWithTrivia2WithConfigureAwait() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = /* comment */ GetNumberAsync()[||] // comment | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = /* comment */ await GetNumberAsync().ConfigureAwait(false) // comment | ||
} | ||
}", index: 1); | ||
} | ||
|
||
[Fact] | ||
public async Task MissingOnSemiColon() | ||
{ | ||
await TestMissingInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
async Task<int> GetNumberAsync() | ||
{ | ||
var x = GetNumberAsync();[||] | ||
} | ||
}"); | ||
} | ||
|
||
[Fact] | ||
public async Task ChainedInvocation() | ||
{ | ||
await TestInRegularAndScriptAsync(@" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
Task<int> GetNumberAsync() => throw null; | ||
async void M() | ||
{ | ||
var x = GetNumberAsync()[||].ToString(); | ||
} | ||
}", @" | ||
using System.Threading.Tasks; | ||
class Program | ||
{ | ||
Task<int> GetNumberAsync() => throw null; | ||
async void M() | ||
{ | ||
var x = (await GetNumberAsync()).ToString(); | ||
} | ||
}"); | ||
} | ||
} | ||
} |
157 changes: 157 additions & 0 deletions
157
src/EditorFeatures/VisualBasicTest/CodeActions/AddAwait/AddAwaitTests.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
Option Strict Off | ||
Imports Microsoft.CodeAnalysis.CodeRefactorings | ||
Imports Microsoft.CodeAnalysis.VisualBasic.CodeRefactorings.AddAwait | ||
|
||
Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.UnitTests.CodeRefactorings.AddAwait | ||
<Trait(Traits.Feature, Traits.Features.AddAwait)> | ||
Public Class AddAwaitTests | ||
Inherits AbstractVisualBasicCodeActionTest | ||
|
||
Protected Overrides Function CreateCodeRefactoringProvider(workspace As Workspace, parameters As TestParameters) As CodeRefactoringProvider | ||
Return New VisualBasicAddAwaitCodeRefactoringProvider() | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function Simple() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = GetNumberAsync()[||] | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Dim expected = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = Await GetNumberAsync() | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestAsync(markup, expected) | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function SimpleWithConfigureAwait() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = GetNumberAsync()[||] | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Dim expected = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = Await GetNumberAsync().ConfigureAwait(False) | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestAsync(markup, expected, index:=1) | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function AlreadyAwaited() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = Await GetNumberAsync()[||] | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestMissingAsync(markup) | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function SimpleWithTrivia() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = GetNumberAsync()[||] ' Comment | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Dim expected = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = Await GetNumberAsync() ' Comment | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestAsync(markup, expected) | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function SimpleWithTriviaAndConfigureAwait() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = GetNumberAsync()[||] ' Comment | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Dim expected = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = Await GetNumberAsync().ConfigureAwait(False) ' Comment | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestAsync(markup, expected, index:=1) | ||
End Function | ||
|
||
<Fact> | ||
Public Async Function ChainedInvocation() As Task | ||
Dim markup = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = GetNumberAsync()[||].ToString() | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Dim expected = | ||
<File> | ||
Imports System.Threading.Tasks | ||
Module Program | ||
Async Function GetNumberAsync() As Task(Of Integer) | ||
Dim x = (Await GetNumberAsync()).ToString() | ||
End Function | ||
End Module | ||
</File> | ||
|
||
Await TestAsync(markup, expected) | ||
End Function | ||
|
||
End Class | ||
End Namespace |
Oops, something went wrong.