Skip to content

Commit

Permalink
Add more nullability awareness in code generation (#36668)
Browse files Browse the repository at this point in the history
Fixes #30324
  • Loading branch information
ryzngard authored Jun 24, 2019
1 parent 670a26e commit 6972d47
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using Xunit.Sdk;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ImplementInterface
{
Expand Down Expand Up @@ -7892,6 +7893,137 @@ public void M<T>() where T : notnull
{
throw new System.NotImplementedException();
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestWithNullableProperty()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
public interface ITest
{
string? P { get; }
}
public class Test : [|ITest|]
{
}",
@"#nullable enable
public interface ITest
{
string? P { get; }
}
public class Test : ITest
{
public string? P => throw new System.NotImplementedException();
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestWithNullablePropertyAlreadyImplemented()
{
await TestMissingAsync(
@"#nullable enable
public interface ITest
{
string? P { get; }
}
public class Test : [|ITest|]
{
public string? P => throw new System.NotImplementedException();
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestWithNullableMethod()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
public interface ITest
{
string? P();
}
public class Test : [|ITest|]
{
}",
@"#nullable enable
public interface ITest
{
string? P();
}
public class Test : ITest
{
public string? P()
{
throw new System.NotImplementedException();
}
}");
}

[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestWithNullableEvent()
{
// Question whether this is needed,
// see https://github.com/dotnet/roslyn/issues/36673
await TestInRegularAndScriptAsync(
@"#nullable enable
using System;
public interface ITest
{
event EventHandler? SomeEvent;
}
public class Test : [|ITest|]
{
}",
@"#nullable enable
using System;
public interface ITest
{
event EventHandler? SomeEvent;
}
public class Test : ITest
{
public event EventHandler? SomeEvent;
}");
}

[Fact(Skip = "https://github.com/dotnet/roslyn/issues/36101"), Trait(Traits.Feature, Traits.Features.CodeActionsImplementInterface)]
public async Task TestWithNullableDisabled()
{
await TestInRegularAndScriptAsync(
@"#nullable enable
public interface ITest
{
string? P { get; }
}
#nullable disable
public class Test : [|ITest|]
{
}",
@"#nullable enable
public interface ITest
{
string? P { get; }
}
#nullable disable
public class Test : ITest
{
public string P => throw new System.NotImplementedException();
}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ internal static IMethodSymbol CreateMethodSymbol(
attributes,
accessibility ?? method.DeclaredAccessibility,
modifiers ?? method.GetSymbolModifiers(),
method.ReturnType,
method.GetReturnTypeWithAnnotatedNullability(),
method.RefKind,
explicitInterfaceImplementations,
name ?? method.Name,
Expand All @@ -460,7 +460,7 @@ internal static IPropertySymbol CreatePropertySymbol(
attributes,
accessibility ?? property.DeclaredAccessibility,
modifiers ?? property.GetSymbolModifiers(),
property.Type,
property.GetTypeWithAnnotatedNullability(),
property.RefKind,
explicitInterfaceImplementations,
name ?? property.Name,
Expand All @@ -484,7 +484,7 @@ internal static IEventSymbol CreateEventSymbol(
attributes,
accessibility ?? @event.DeclaredAccessibility,
modifiers ?? @event.GetSymbolModifiers(),
@event.Type,
@event.GetTypeWithAnnotatedNullability(),
explicitInterfaceImplementations,
name ?? @event.Name,
addMethod,
Expand Down

0 comments on commit 6972d47

Please sign in to comment.