未验证 提交 6f80480d 编写于 作者: A Andrew Hall 提交者: GitHub

Add tests for C# simplification around NRT (#36878)

上级 e4d465e2
......@@ -2,6 +2,7 @@
Imports System.Collections.Immutable
Imports Microsoft.CodeAnalysis
Imports Microsoft.CodeAnalysis.CSharp
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Extensions
Imports Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces
Imports Microsoft.CodeAnalysis.Options
......@@ -13,30 +14,41 @@ Namespace Microsoft.CodeAnalysis.Editor.UnitTests.Simplification
<[UseExportProvider]>
Public MustInherit Class AbstractSimplificationTests
Protected Async Function TestAsync(definition As XElement, expected As XElement, Optional options As Dictionary(Of OptionKey, Object) = Nothing) As System.Threading.Tasks.Task
Protected Async Function TestAsync(definition As XElement, expected As XElement, Optional options As Dictionary(Of OptionKey, Object) = Nothing, Optional csharpParseOptions As CSharpParseOptions = Nothing) As System.Threading.Tasks.Task
Using workspace = TestWorkspace.Create(definition)
Dim hostDocument = workspace.Documents.Single()
Dim finalWorkspace = workspace
Dim spansToAddSimplifierAnnotation = hostDocument.AnnotatedSpans.Where(Function(kvp) kvp.Key.StartsWith("Simplify", StringComparison.Ordinal))
Dim explicitSpanToSimplifyAnnotatedSpans = hostDocument.AnnotatedSpans.Where(Function(kvp) Not spansToAddSimplifierAnnotation.Contains(kvp))
If explicitSpanToSimplifyAnnotatedSpans.Count <> 1 OrElse explicitSpanToSimplifyAnnotatedSpans.Single().Key <> "SpanToSimplify" Then
For Each span In explicitSpanToSimplifyAnnotatedSpans
If span.Key <> "SpanToSimplify" Then
Assert.True(False, "Encountered unexpected span annotation: " + span.Key)
End If
If csharpParseOptions IsNot Nothing Then
For Each project In workspace.CurrentSolution.Projects
finalWorkspace.ChangeSolution(finalWorkspace.CurrentSolution.WithProjectParseOptions(project.Id, csharpParseOptions))
Next
End If
Dim explicitSpansToSimplifyWithin = If(explicitSpanToSimplifyAnnotatedSpans.Any(),
explicitSpanToSimplifyAnnotatedSpans.Single().Value,
Nothing)
Await TestAsync(
workspace, spansToAddSimplifierAnnotation,
explicitSpansToSimplifyWithin, expected, options)
Await TestAsync(finalWorkspace, expected, options).ConfigureAwait(False)
End Using
End Function
Protected Async Function TestAsync(workspace As TestWorkspace, expected As XElement, Optional options As Dictionary(Of OptionKey, Object) = Nothing) As System.Threading.Tasks.Task
Dim hostDocument = workspace.Documents.Single()
Dim spansToAddSimplifierAnnotation = hostDocument.AnnotatedSpans.Where(Function(kvp) kvp.Key.StartsWith("Simplify", StringComparison.Ordinal))
Dim explicitSpanToSimplifyAnnotatedSpans = hostDocument.AnnotatedSpans.Where(Function(kvp) Not spansToAddSimplifierAnnotation.Contains(kvp))
If explicitSpanToSimplifyAnnotatedSpans.Count <> 1 OrElse explicitSpanToSimplifyAnnotatedSpans.Single().Key <> "SpanToSimplify" Then
For Each span In explicitSpanToSimplifyAnnotatedSpans
If span.Key <> "SpanToSimplify" Then
Assert.True(False, "Encountered unexpected span annotation: " + span.Key)
End If
Next
End If
Dim explicitSpansToSimplifyWithin = If(explicitSpanToSimplifyAnnotatedSpans.Any(),
explicitSpanToSimplifyAnnotatedSpans.Single().Value,
Nothing)
Await TestAsync(
workspace, spansToAddSimplifierAnnotation,
explicitSpansToSimplifyWithin, expected, options)
End Function
Private Async Function TestAsync(workspace As Workspace,
......
......@@ -5035,6 +5035,128 @@ class C
Await TestAsync(input, expected)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestCSharp_DoNotSimplifyNullableGeneric() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
#nullable enable
using System.Threading.Tasks;
class Program
{
Task<string?> M()
{
string s1 = "test";
return {|Simplify:Task.FromResult<string?>|}(s1);
}
}
]]>
</Document>
</Project>
</Workspace>
Dim expected =
<code><![CDATA[
#nullable enable
using System.Threading.Tasks;
class Program
{
Task<string?> M()
{
string s1 = "test";
return Task.FromResult<string?>(s1);
}
}
]]>
</code>
Await TestAsync(input, expected)
End Function
<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestCSharp_SimplifyNullableWithNullableSuppressionOperator() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
#nullable enable
class Program
{
void M()
{
string? s1 = null;
string s2 = {|Simplify:M1<string>|}(s1!, "hello");
}
static T M1<T>(T t1, T t2) where T : class? =>
t1 ?? t2;
}
]]>
</Document>
</Project>
</Workspace>
Dim expected =
<code><![CDATA[
#nullable enable
class Program
{
void M()
{
string? s1 = null;
string s2 = M1(s1!, "hello");
}
static T M1<T>(T t1, T t2) where T : class? =>
t1 ?? t2;
}
]]>
</code>
Await TestAsync(input, expected)
End Function
<Fact(Skip:="https://github.com/dotnet/roslyn/issues/36884"), Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestCSharp_SimplifyNullableMethodTypeArgument() As Task
Dim input =
<Workspace>
<Project Language="C#" CommonReferences="true">
<Document><![CDATA[
#nullable enable
class Program
{
void M()
{
string? s1 = null;
string? s2 = {|Simplify:M1<string?>|}(s1);
}
static T M1<T>(T t) where T : class? => t;
}
]]>
</Document>
</Project>
</Workspace>
Dim expected =
<code><![CDATA[
#nullable enable
class Program
{
void M()
{
string? s1 = null;
string? s2 = M1(s1);
}
static T M1<T>(T t) where T : class? => t;
}
]]>
</code>
Await TestAsync(input, expected)
End Function
#End Region
#Region "Visual Basic tests"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册