Skip to content

Commit

Permalink
Add some nullability tests for Generate Type and fix one bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmalinowski committed Jun 20, 2019
1 parent 56b512b commit 3875c71
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3875c71

Please sign in to comment.