Skip to content

Commit 7f931cd

Browse files
committed
Add Mapperly tests
1 parent 12c9d12 commit 7f931cd

3 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/plugins/MapperlyTest/DogMapper.cs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal sealed partial class DogMapper
6767
[MapProperty(nameof(Dog.NullStr), nameof(DogDto.StringNull))]
6868
internal partial DogDto ToDogDto(Dog dog);
6969

70-
internal partial ICollection<DogDto> ToDogDto(ICollection<Dog> dogs);
70+
internal partial ICollection<DogDto> ToDogDtos(ICollection<Dog> dogs);
7171

7272
#endregion
7373

@@ -79,11 +79,51 @@ internal static partial class DogProjectToMapper
7979

8080
#region Constants & Statics
8181

82-
[MapProperty(nameof(Dog.NullStr), nameof(DogDto.StringNull))]
82+
[IncludeMappingConfiguration(nameof(@DogMapper.ToDogDto))]
8383
private static partial DogDto ToDogDto(Dog dog);
8484

8585
internal static partial IQueryable<DogDto> ProjectToDogDto(this IQueryable<Dog> query);
8686

8787
#endregion
8888

8989
}
90+
91+
[Mapper]
92+
internal static partial class DogProjectToWithCustomMapper
93+
{
94+
95+
#region Constants & Statics
96+
97+
private static string StringNullSet(string? source)
98+
{
99+
return source ?? "default str";
100+
}
101+
102+
[MapProperty(nameof(Dog.NullStr), nameof(DogDto.StringNull), Use = nameof(StringNullSet))]
103+
private static partial DogDto ToDogDto(Dog dog);
104+
105+
internal static partial IQueryable<DogDto> ProjectToDogDto2(this IQueryable<Dog> query);
106+
107+
#endregion
108+
109+
}
110+
111+
[Mapper]
112+
internal static partial class DogDtoExtensionsMapper
113+
{
114+
115+
#region Constants & Statics
116+
117+
private static string StringNullSet(Dog dog)
118+
{
119+
return dog.NullStr ?? "default str";
120+
}
121+
122+
[MapPropertyFromSource(nameof(DogDto.StringNull), Use = nameof(StringNullSet))]
123+
[MapperIgnoreTarget(nameof(DogDto.Name))]
124+
[MapperIgnoreTarget(nameof(DogDto.NumberOfWheels))]
125+
internal static partial void FromDog([MappingTarget] this DogDto dogDto, Dog dog);
126+
127+
#endregion
128+
129+
}

src/plugins/MapperlyTest/MapperTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public static void Ctro_Test()
3434
Console.WriteLine($"StringNull: {dto.StringNull}");
3535
}
3636

37+
public static void ExistingTarget_Test()
38+
{
39+
var dog = new Dog { Name = "Fiat", NumberOfWheels = 4, NullStr = "aaa" };
40+
var dto = new DogDto { Name = "OldName", NumberOfWheels = 99, StringNull = "OldString" };
41+
dto.FromDog(dog);
42+
43+
Console.WriteLine($"{dto.Name}, Wheels: {dto.NumberOfWheels}");
44+
Console.WriteLine($"StringNull: {dto.StringNull}");
45+
}
46+
3747
public static void List_Test()
3848
{
3949
var mapper = new DogMapper();
@@ -44,7 +54,7 @@ public static void List_Test()
4454
new() { Name = "Max", NumberOfWheels = 3, NullStr = "bbb" }
4555
};
4656

47-
var list = mapper.ToDogDto(dogs);
57+
var list = mapper.ToDogDtos(dogs);
4858

4959
foreach (var dto in list)
5060
{

src/plugins/MapperlyTest/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ private static void Main(string[] args)
1010
//MapperTest.CarMapper_Test();
1111
//MapperTest.CarNullThrowMapper_Test();
1212
//MapperTest.Ctro_Test();
13-
MapperTest.ProjectTo_Test();
13+
//MapperTest.ProjectTo_Test();
14+
MapperTest.ExistingTarget_Test();
1415
}
1516

1617
#endregion

0 commit comments

Comments
 (0)