Skip to content

Commit

Permalink
Create AddAwait refactoring (dotnet#28930)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Sep 13, 2018
1 parent 5709488 commit 0870135
Show file tree
Hide file tree
Showing 40 changed files with 875 additions and 0 deletions.
227 changes: 227 additions & 0 deletions src/EditorFeatures/CSharpTest/CodeActions/AddAwait/AddAwaitTests.cs
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();
}
}");
}
}
}
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
Loading

0 comments on commit 0870135

Please sign in to comment.