Skip to content

Commit

Permalink
add test with nullable initialization outside of container
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz committed Jan 20, 2025
1 parent ffd5592 commit fb60aa3
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyNullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,42 @@ TestSourceBuilderOptions.Default with
);
}

[Fact]
public void NullableNestedMembersShouldInitializeWithNoNullAssignmentOutsideContainer()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapProperty("NullableValue1", "V.NullableValue1")]
[MapProperty("Value2", "V.Value2")]
public partial B Map(A a)
""",
TestSourceBuilderOptions.Default with
{
AllowNullPropertyAssignment = false,
},
"class A { public int? NullableValue1 { get; set; } public int Value2 { get; set; } }",
"class B { public C? V { get; set; } }",
"class C { public int? NullableValue1 { get; set; } public int? Value2 { get; set; } }"
);

TestHelper
.GenerateMapper(source)
.Should()
.HaveMapMethodBody(
"""
var target = new global::B();
if (a.NullableValue1 != null)
{
target.V ??= new global::C();
target.V.NullableValue1 = a.NullableValue1.Value;
}
target.V ??= new global::C();
target.V.Value2 = a.Value2;
return target;
"""
);
}

[Fact]
public void NullableClassToNullableClassPropertyThrowShouldSetNull()
{
Expand Down

0 comments on commit fb60aa3

Please sign in to comment.