From ce722408cf7a96b7cbe957ce131997bd37694ee8 Mon Sep 17 00:00:00 2001 From: daver32 <38791383+daver32@users.noreply.github.com> Date: Sat, 14 Aug 2021 13:38:21 +0200 Subject: [PATCH] init properties support --- .../AccessorsGenerationTests.cs | 21 ++++++++++++++++++- InterfaceGenerator/AutoInterfaceGenerator.cs | 9 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/InterfaceGenerator.Tests/AccessorsGenerationTests.cs b/InterfaceGenerator.Tests/AccessorsGenerationTests.cs index 9d6aa61..dfe68e8 100644 --- a/InterfaceGenerator.Tests/AccessorsGenerationTests.cs +++ b/InterfaceGenerator.Tests/AccessorsGenerationTests.cs @@ -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; } diff --git a/InterfaceGenerator/AutoInterfaceGenerator.cs b/InterfaceGenerator/AutoInterfaceGenerator.cs index 1e1a290..f3ddd0d 100644 --- a/InterfaceGenerator/AutoInterfaceGenerator.cs +++ b/InterfaceGenerator/AutoInterfaceGenerator.cs @@ -254,7 +254,14 @@ namespace InterfaceGenerator if (hasPublicSetter) { - writer.Write("set; "); + if (propertySymbol.SetMethod!.IsInitOnly) + { + writer.Write("init; "); + } + else + { + writer.Write("set; "); + } } writer.WriteLine("}");