init properties support

This commit is contained in:
daver32
2021-08-14 13:38:21 +02:00
parent 35f8b0a159
commit ce722408cf
2 changed files with 28 additions and 2 deletions
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Runtime.CompilerServices;
using FluentAssertions;
using FluentAssertions.Common;
using Xunit;
@@ -42,6 +43,22 @@ namespace InterfaceGenerator.Tests
_sut.PublicProperty = string.Empty;
}
[Fact]
public void InitProperty_IsImplemented()
{
var prop = typeof(IAccessorsTestsService)
.GetProperty(nameof(IAccessorsTestsService.InitOnlyProperty));
prop.Should().NotBeNull();
prop.GetMethod.Should().NotBeNull();
prop.SetMethod.Should().NotBeNull();
prop.SetMethod.ReturnParameter.GetRequiredCustomModifiers().Should().Contain(typeof(IsExternalInit));
string _ = _sut.InitOnlyProperty;
}
[Fact]
public void PrivateSetter_IsOmitted()
{
@@ -128,6 +145,8 @@ namespace InterfaceGenerator.Tests
}
public string PublicProperty { get; set; }
public string InitOnlyProperty { get; init; }
public string PropertyWithPrivateSetter { get; private set; }
+8 -1
View File
@@ -254,7 +254,14 @@ namespace InterfaceGenerator
if (hasPublicSetter)
{
writer.Write("set; ");
if (propertySymbol.SetMethod!.IsInitOnly)
{
writer.Write("init; ");
}
else
{
writer.Write("set; ");
}
}
writer.WriteLine("}");