From 3875c718a18cf881ee6d6e1b5a334fb27f5b5e3d Mon Sep 17 00:00:00 2001 From: Jason Malinowski Date: Wed, 19 Jun 2019 18:10:08 -0700 Subject: [PATCH] Add some nullability tests for Generate Type and fix one bug Closes https://github.com/dotnet/roslyn/issues/30316 --- .../GenerateType/GenerateTypeTests.cs | 128 ++++++++++++++++++ .../AbstractGenerateTypeService.State.cs | 4 +- 2 files changed, 131 insertions(+), 1 deletion(-) diff --git a/src/EditorFeatures/CSharpTest/Diagnostics/GenerateType/GenerateTypeTests.cs b/src/EditorFeatures/CSharpTest/Diagnostics/GenerateType/GenerateTypeTests.cs index ff99452725f7b..845e231600e4e 100644 --- a/src/EditorFeatures/CSharpTest/Diagnostics/GenerateType/GenerateTypeTests.cs +++ b/src/EditorFeatures/CSharpTest/Diagnostics/GenerateType/GenerateTypeTests.cs @@ -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() { diff --git a/src/Features/Core/Portable/GenerateType/AbstractGenerateTypeService.State.cs b/src/Features/Core/Portable/GenerateType/AbstractGenerateTypeService.State.cs index 584201792c09e..2985be05f98ba 100644 --- a/src/Features/Core/Portable/GenerateType/AbstractGenerateTypeService.State.cs +++ b/src/Features/Core/Portable/GenerateType/AbstractGenerateTypeService.State.cs @@ -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. + this.BaseTypeOrInterfaceOpt = baseType.WithNullability(NullableAnnotation.NotApplicable); } private bool GenerateStruct(TService service, SemanticModel semanticModel, CancellationToken cancellationToken)