未验证 提交 07287fa4 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #36605 from jasonmalinowski/add-tests-for-generate-type

Add some nullability tests for Generate Type and fix one bug
......@@ -444,6 +444,27 @@ private class Goo
index: 2);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task TestGenerateClassFromNullableFieldDeclarationIntoSameType()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
class Class
{
[|Goo?|] f;
}",
@"#nullable enable
class Class
{
Goo? f;
private class Goo
{
}
}",
index: 2);
}
[WpfFact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task TestGenerateClassFromFieldDeclarationIntoGlobalNamespace()
{
......@@ -1385,6 +1406,78 @@ public T(int v1, string v2)
index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task GenerateWithNullableParameter()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
class Class
{
void M()
{
string? s = null;
new [|T|](s);
}
}",
@"#nullable enable
class Class
{
void M()
{
string? s = null;
new [|T|](s);
}
}
internal class T
{
private string? s;
public T(string? s)
{
this.s = s;
}
}",
parseOptions: TestOptions.Regular8WithNullableAnalysis,
index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task GenerateWithNullableParameterThatIsNotNull()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
class Class
{
void M()
{
string? s = ""asdf"";
new [|T|](s);
}
}",
@"#nullable enable
class Class
{
void M()
{
string? s = ""asdf"";
new [|T|](s);
}
}
internal class T
{
private string s;
public T(string s)
{
this.s = s;
}
}",
parseOptions: TestOptions.Regular8WithNullableAnalysis,
index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task GenerateWithNamedParameter()
{
......@@ -2109,6 +2202,41 @@ protected Base(out int o)
index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task GenerateWithDelegatingConstructorAssigningToNullableField()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
class Class
{
void M()
{
Base? b = new [|T|]();
}
}
class Base
{
}",
@"#nullable enable
class Class
{
void M()
{
Base? b = new [|T|]();
}
}
internal class T : Base
{
}
class Base
{
}",
index: 1);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsGenerateType)]
public async Task GenerateWithNonDelegatingConstructor1()
{
......
......@@ -245,7 +245,9 @@ private void SetBaseType(INamedTypeSymbol baseType)
return;
}
this.BaseTypeOrInterfaceOpt = baseType;
// Strip off top-level nullability since we can't put top-level nullability into the base list. We will still include nested nullability
// if you're deriving some interface like IEnumerable<string?>.
this.BaseTypeOrInterfaceOpt = baseType.WithNullability(NullableAnnotation.NotApplicable);
}
private bool GenerateStruct(TService service, SemanticModel semanticModel, CancellationToken cancellationToken)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册