Skip to content

Commit

Permalink
feat: Support C#12 primary constructors (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz authored Sep 14, 2023
1 parent c2f1f4f commit b54b9a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/CtorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b54b9a5

Please sign in to comment.