From 2221c03dcbc3aeeb4809356342a3e9fa35aed241 Mon Sep 17 00:00:00 2001 From: latonz Date: Tue, 5 Sep 2023 10:07:45 +0200 Subject: [PATCH] feat: Support C#12 primary constructors --- test/Riok.Mapperly.Tests/Mapping/CtorTest.cs | 7 +++++++ .../ObjectPropertyConstructorResolverTest.cs | 21 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/test/Riok.Mapperly.Tests/Mapping/CtorTest.cs b/test/Riok.Mapperly.Tests/Mapping/CtorTest.cs index 4a98082891..e7517dbcef 100644 --- a/test/Riok.Mapperly.Tests/Mapping/CtorTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/CtorTest.cs @@ -19,6 +19,13 @@ public void CtorCustomStruct() TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return new global::A(source);"); } + [Fact] + public void PrimaryCtorCustomClass() + { + var source = TestSourceBuilder.Mapping("string", "A", "class A(string x) {}"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return new global::A(source);"); + } + [Fact] public void CtorMappingDisabledShouldDiagnostic() { diff --git a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs index 5127610d28..d79e892eec 100644 --- a/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/ObjectPropertyConstructorResolverTest.cs @@ -484,6 +484,27 @@ public void CanResolveToClassConstructorWithMapPropertyAttribute() ); } + [Fact] + public void CanResolveToClassPrimaryConstructor() + { + var source = TestSourceBuilder.Mapping( + "A", + "B", + "class A(string? id, bool value) { public string? Id => id; public bool Value => value; }", + "class B(string? id, bool value) {}" + ); + + var result = TestHelper.GenerateMapper(source); + result + .Should() + .HaveSingleMethodBody( + """ + var target = new global::B(source.Id, source.Value); + return target; + """ + ); + } + [Fact] public void RecordToRecordNullableToNullableEnum() {