提交 c9bb66f3 编写于 作者: C Cyrus Najmabadi

Make formatting tests async.

上级 ddb81303
......@@ -334,13 +334,13 @@ private bool TryCreateUpdatedSolution(ChangeSignatureAnalyzedContext context, Ch
var annotatedNodes = newRoot.GetAnnotatedNodes<SyntaxNode>(syntaxAnnotation: changeSignatureFormattingAnnotation);
var formattedRoot = Formatter.Format(
var formattedRoot = Formatter.FormatAsync(
newRoot,
changeSignatureFormattingAnnotation,
doc.Project.Solution.Workspace,
options: null,
rules: GetFormattingRules(doc),
cancellationToken: CancellationToken.None);
cancellationToken: CancellationToken.None).WaitAndGetResult(CancellationToken.None);
updatedRoots[docId] = formattedRoot;
}
......
......@@ -218,7 +218,7 @@ public static string GetEventHandlerMemberId(Document document, string className
var formattingRules = additionalFormattingRule.Concat(Formatter.GetDefaultFormattingRules(targetDocument));
var workspace = targetDocument.Project.Solution.Workspace;
newRoot = Formatter.Format(newRoot, Formatter.Annotation, workspace, workspace.Options, formattingRules, cancellationToken);
newRoot = Formatter.FormatAsync(newRoot, Formatter.Annotation, workspace, workspace.Options, formattingRules, cancellationToken).WaitAndGetResult(cancellationToken);
var newMember = newRoot.GetAnnotatedNodesAndTokens(annotation).Single();
var newMemberText = newMember.ToFullString();
......
// 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.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.UnitTests.Formatting;
......@@ -17,7 +18,7 @@ protected override SyntaxNode ParseCompilation(string text, ParseOptions parseOp
return SyntaxFactory.ParseCompilationUnit(text, options: (CSharpParseOptions)parseOptions);
}
protected void AssertFormat(
protected Task AssertFormatAsync(
string expected,
string code,
bool debugMode = false,
......@@ -25,10 +26,10 @@ protected override SyntaxNode ParseCompilation(string text, ParseOptions parseOp
bool testWithTransformation = true,
ParseOptions parseOptions = null)
{
AssertFormat(expected, code, SpecializedCollections.SingletonEnumerable(new TextSpan(0, code.Length)), debugMode, changedOptionSet, testWithTransformation, parseOptions);
return AssertFormatAsync(expected, code, SpecializedCollections.SingletonEnumerable(new TextSpan(0, code.Length)), debugMode, changedOptionSet, testWithTransformation, parseOptions);
}
protected void AssertFormat(
protected Task AssertFormatAsync(
string expected,
string code,
IEnumerable<TextSpan> spans,
......@@ -37,7 +38,7 @@ protected override SyntaxNode ParseCompilation(string text, ParseOptions parseOp
bool testWithTransformation = true,
ParseOptions parseOptions = null)
{
AssertFormat(expected, code, spans, LanguageNames.CSharp, debugMode, changedOptionSet, testWithTransformation, parseOptions);
return AssertFormatAsync(expected, code, spans, LanguageNames.CSharp, debugMode, changedOptionSet, testWithTransformation, parseOptions);
}
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Formatting;
......@@ -18,22 +19,22 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Formatting
public class FormattingEngineMultiSpanTests : CSharpFormattingTestBase
{
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void EndOfLine()
public async Task EndOfLine()
{
var content = @"namespace A{/*1*/}/*2*/";
var expected = @"namespace A{}";
AssertFormat(content, expected);
await AssertFormatAsync(content, expected).ConfigureAwait(true);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void Simple1()
public async Task Simple1()
{
AssertFormat("namespace A/*1*/{}/*2*/ class A {}", "namespace A{ } class A {}");
await AssertFormatAsync("namespace A/*1*/{}/*2*/ class A {}", "namespace A{ } class A {}").ConfigureAwait(true);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void DontFormatTriviaOutsideOfSpan_IncludingTrailingTriviaOnNewLine()
public async Task DontFormatTriviaOutsideOfSpan_IncludingTrailingTriviaOnNewLine()
{
var content = @"namespace A
/*1*/{
......@@ -47,11 +48,11 @@ class A /*1*/{}/*2*/";
class A { }";
AssertFormat(content, expected);
await AssertFormatAsync(content, expected).ConfigureAwait(true);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatIncludingTrivia()
public async Task FormatIncludingTrivia()
{
var content = @"namespace A
/*1*/{
......@@ -65,11 +66,11 @@ class A /*1*/{}/*2*/";
class A { }";
AssertFormat(content, expected);
await AssertFormatAsync(content, expected).ConfigureAwait(true);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void MergeSpanAndFormat()
public async Task MergeSpanAndFormat()
{
var content = @"namespace A
/*1*/{
......@@ -83,11 +84,11 @@ class A{}/*2*/";
class A { }";
AssertFormat(content, expected);
await AssertFormatAsync(content, expected).ConfigureAwait(true);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public void OverlappedSpan()
public async Task OverlappedSpan()
{
var content = @"namespace A
/*1*/{
......@@ -101,13 +102,13 @@ class A{}/*2*/";
class A { }";
AssertFormat(content, expected);
await AssertFormatAsync(content, expected).ConfigureAwait(true);
}
[Fact]
[WorkItem(554160, "DevDiv")]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatSpanNullReference01()
public async Task FormatSpanNullReference01()
{
var code = @"/*1*/class C
{
......@@ -126,13 +127,13 @@ void F()
}";
var changingOptions = new Dictionary<OptionKey, object>();
changingOptions.Add(CSharpFormattingOptions.IndentBlock, false);
AssertFormat(code, expected, changedOptionSet: changingOptions);
await AssertFormatAsync(code, expected, changedOptionSet: changingOptions).ConfigureAwait(true);
}
[Fact]
[WorkItem(554160, "DevDiv")]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public void FormatSpanNullReference02()
public async Task FormatSpanNullReference02()
{
var code = @"class C/*1*/
{
......@@ -151,7 +152,7 @@ void F()
}";
var changingOptions = new Dictionary<OptionKey, object>();
changingOptions.Add(CSharpFormattingOptions.WrappingPreserveSingleLine, false);
AssertFormat(code, expected, changedOptionSet: changingOptions);
await AssertFormatAsync(code, expected, changedOptionSet: changingOptions).ConfigureAwait(true);
}
[WorkItem(539231, "DevDiv")]
......@@ -168,11 +169,11 @@ public void EmptySpan()
}
}
private void AssertFormat(string content, string expected, Dictionary<OptionKey, object> changedOptionSet = null)
private Task AssertFormatAsync(string content, string expected, Dictionary<OptionKey, object> changedOptionSet = null)
{
var tuple = PreprocessMarkers(content);
AssertFormat(expected, tuple.Item1, tuple.Item2, changedOptionSet: changedOptionSet);
return AssertFormatAsync(expected, tuple.Item1, tuple.Item2, changedOptionSet: changedOptionSet);
}
private Tuple<string, List<TextSpan>> PreprocessMarkers(string codeWithMarker)
......
......@@ -162,7 +162,7 @@ internal static async Task<Document> FormatAsync(Document document, SyntaxAnnota
}
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return document.WithSyntaxRoot(Format(root, annotation, document.Project.Solution.Workspace, options, rules, cancellationToken));
return document.WithSyntaxRoot(await FormatAsync(root, annotation, document.Project.Solution.Workspace, options, rules, cancellationToken).ConfigureAwait(false));
}
/// <summary>
......@@ -176,10 +176,15 @@ internal static async Task<Document> FormatAsync(Document document, SyntaxAnnota
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Format(node, annotation, workspace, options, rules: null, cancellationToken: cancellationToken);
return FormatAsync(node, annotation, workspace, options, cancellationToken).WaitAndGetResult(cancellationToken);
}
internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return FormatAsync(node, annotation, workspace, options, rules: null, cancellationToken: cancellationToken);
}
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
if (workspace == null)
{
......@@ -200,7 +205,7 @@ internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation,
? GetElasticSpans(node)
: GetAnnotatedSpans(node, annotation);
return Format(node, spans, workspace, options, rules, cancellationToken);
return FormatAsync(node, spans, workspace, options, rules, cancellationToken);
}
/// <summary>
......@@ -294,7 +299,12 @@ internal static async Task<SyntaxNode> FormatAsync(SyntaxNode node, IEnumerable<
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
return GetFormattedTextChangesAsync(node, workspace, options, cancellationToken).WaitAndGetResult(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChangesAsync(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
}
/// <summary>
......@@ -308,7 +318,12 @@ public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, Workspa
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
return GetFormattedTextChangesAsync(node, span, workspace, options, cancellationToken).WaitAndGetResult(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChangesAsync(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
}
/// <summary>
......@@ -322,7 +337,12 @@ public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, TextSpa
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChanges(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
return GetFormattedTextChangesAsync(node, spans, workspace, options, cancellationToken).WaitAndGetResult(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetFormattedTextChangesAsync(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
}
internal static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
......
......@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
......@@ -12,7 +13,7 @@ namespace Microsoft.CodeAnalysis.UnitTests.Formatting
{
public abstract class FormattingTestBase
{
protected void AssertFormat(
protected Task AssertFormatAsync(
string expected,
string code,
string language,
......@@ -20,10 +21,10 @@ public abstract class FormattingTestBase
Dictionary<OptionKey, object> changedOptionSet = null,
bool testWithTransformation = true)
{
AssertFormat(expected, code, SpecializedCollections.SingletonEnumerable(new TextSpan(0, code.Length)), language, debugMode, changedOptionSet, testWithTransformation);
return AssertFormatAsync(expected, code, SpecializedCollections.SingletonEnumerable(new TextSpan(0, code.Length)), language, debugMode, changedOptionSet, testWithTransformation);
}
protected void AssertFormat(
protected async Task AssertFormatAsync(
string expected,
string code,
IEnumerable<TextSpan> spans,
......@@ -55,7 +56,7 @@ public abstract class FormattingTestBase
}
var root = syntaxTree.GetRoot();
AssertFormat(workspace, expected, root, spans, options, document.GetTextAsync().Result);
await AssertFormatAsync(workspace, expected, root, spans, options, await document.GetTextAsync().ConfigureAwait(true)).ConfigureAwait(true);
// format with node and transform
AssertFormatWithTransformation(workspace, expected, root, spans, options, treeCompare, parseOptions);
......@@ -81,9 +82,9 @@ public abstract class FormattingTestBase
}
}
protected static void AssertFormat(Workspace workspace, string expected, SyntaxNode root, IEnumerable<TextSpan> spans, OptionSet optionSet, SourceText sourceText)
protected static async Task AssertFormatAsync(Workspace workspace, string expected, SyntaxNode root, IEnumerable<TextSpan> spans, OptionSet optionSet, SourceText sourceText)
{
var result = Formatter.GetFormattedTextChanges(root, spans, workspace, optionSet);
var result = await Formatter.GetFormattedTextChangesAsync(root, spans, workspace, optionSet).ConfigureAwait(true);
AssertResult(expected, sourceText, result);
}
......
......@@ -30,14 +30,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Formatting
Return StringFromLines(adjustedLines.ToArray())
End Function
Protected Sub AssertFormatLf2CrLf(code As String, expected As String, Optional optionSet As Dictionary(Of OptionKey, Object) = Nothing)
Protected Function AssertFormatLf2CrLfAsync(code As String, expected As String, Optional optionSet As Dictionary(Of OptionKey, Object) = Nothing) As Task
code = code.Replace(vbLf, vbCrLf)
expected = expected.Replace(vbLf, vbCrLf)
AssertFormat(code, expected, changedOptionSet:=optionSet)
End Sub
Return AssertFormatAsync(code, expected, changedOptionSet:=optionSet)
End Function
Protected Sub AssertFormatUsingAllEntryPoints(code As String, expected As String)
Protected Async Function AssertFormatUsingAllEntryPointsAsync(code As String, expected As String) As Task
Using workspace = New AdhocWorkspace()
Dim project = workspace.CurrentSolution.AddProject("Project", "Project.dll", LanguageNames.VisualBasic)
......@@ -49,50 +49,50 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Formatting
Dim spans = New List(Of TextSpan)()
spans.Add(syntaxTree.GetRoot().FullSpan)
Dim changes = Formatter.GetFormattedTextChanges(syntaxTree.GetRoot(CancellationToken.None), workspace, cancellationToken:=CancellationToken.None)
Dim changes = Await Formatter.GetFormattedTextChangesAsync(syntaxTree.GetRoot(CancellationToken.None), workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
AssertResult(expected, document.GetTextAsync().Result, changes)
changes = Formatter.GetFormattedTextChanges(syntaxTree.GetRoot(), syntaxTree.GetRoot(CancellationToken.None).FullSpan, workspace, cancellationToken:=CancellationToken.None)
changes = Await Formatter.GetFormattedTextChangesAsync(syntaxTree.GetRoot(), syntaxTree.GetRoot(CancellationToken.None).FullSpan, workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
AssertResult(expected, document.GetTextAsync().Result, changes)
spans = New List(Of TextSpan)()
spans.Add(syntaxTree.GetRoot().FullSpan)
changes = Formatter.GetFormattedTextChanges(syntaxTree.GetRoot(CancellationToken.None), spans, workspace, cancellationToken:=CancellationToken.None)
changes = Await Formatter.GetFormattedTextChangesAsync(syntaxTree.GetRoot(CancellationToken.None), spans, workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
AssertResult(expected, document.GetTextAsync().Result, changes)
' format with node and transform
AssertFormatWithTransformation(workspace, expected, syntaxTree.GetRoot(), spans, Nothing, False)
End Using
End Sub
End Function
Protected Sub AssertFormatSpan(markupCode As String, expected As String)
Protected Function AssertFormatSpanAsync(markupCode As String, expected As String) As Task
Dim code As String = Nothing
Dim cursorPosition As Integer? = Nothing
Dim spans As IList(Of TextSpan) = Nothing
MarkupTestFile.GetSpans(markupCode, code, spans)
AssertFormat(expected, code, spans)
End Sub
Return AssertFormatAsync(expected, code, spans)
End Function
Protected Overloads Sub AssertFormat(
Protected Overloads Function AssertFormatAsync(
code As String,
expected As String,
Optional debugMode As Boolean = False,
Optional changedOptionSet As Dictionary(Of OptionKey, Object) = Nothing,
Optional testWithTransformation As Boolean = False,
Optional experimental As Boolean = False)
AssertFormat(expected, code, SpecializedCollections.SingletonEnumerable(New TextSpan(0, code.Length)), debugMode, changedOptionSet, testWithTransformation, experimental:=experimental)
End Sub
Optional experimental As Boolean = False) As Task
Return AssertFormatAsync(expected, code, SpecializedCollections.SingletonEnumerable(New TextSpan(0, code.Length)), debugMode, changedOptionSet, testWithTransformation, experimental:=experimental)
End Function
Protected Overloads Sub AssertFormat(
Protected Overloads Function AssertFormatAsync(
expected As String,
code As String,
spans As IEnumerable(Of TextSpan),
Optional debugMode As Boolean = False,
Optional changedOptionSet As Dictionary(Of OptionKey, Object) = Nothing,
Optional testWithTransformation As Boolean = False,
Optional experimental As Boolean = False)
Optional experimental As Boolean = False) As Task
Dim parseOptions = New VisualBasicParseOptions()
If (experimental) Then
......@@ -100,8 +100,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Formatting
' parseOptions = parseOptions.WithExperimentalFeatures
End If
AssertFormat(expected, code, spans, LanguageNames.VisualBasic, debugMode, changedOptionSet, testWithTransformation, parseOptions)
End Sub
Return AssertFormatAsync(expected, code, spans, LanguageNames.VisualBasic, debugMode, changedOptionSet, testWithTransformation, parseOptions)
End Function
Private Function StringFromLines(ParamArray lines As String()) As String
Return String.Join(Environment.NewLine, lines)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册