-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathWhenIncludeDerivedClasses.cs
More file actions
106 lines (93 loc) · 2.96 KB
/
Copy pathWhenIncludeDerivedClasses.cs
File metadata and controls
106 lines (93 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
namespace Mapster.Tests
{
[TestClass]
public class WhenIncludeDerivedClasses
{
[TestMethod]
public void Map_Including_Derived_Class()
{
TypeAdapterConfig<Vehicle, VehicleDto>.NewConfig()
.Include<Car, CarDto>()
.Compile();
Vehicle vehicle = new Car { Id = 1, Name = "Car", Make = "Toyota", ChassiNumber = "XXX" };
var dto = vehicle.Adapt<Vehicle, VehicleDto>();
dto.ShouldBeOfType<CarDto>();
((CarDto)dto).Make.ShouldBe("Toyota");
}
[TestMethod]
public void Map_Including_Derived_Class_With_List()
{
TypeAdapterConfig<Vehicle, VehicleDto>.NewConfig()
.Include<Car, CarDto>()
.Include<Bike, BikeDto>()
.Compile();
var vehicles = new List<Vehicle>
{
new Car {Id = 1, Name = "Car", Make = "Toyota", ChassiNumber = "XXX"},
new Bike {Id = 2, Name = "Bike", Brand = "BMX"},
};
var dto = vehicles.Adapt<List<Vehicle>, IList<VehicleDto>>();
((CarDto)dto[0]).Make.ShouldBe("Toyota");
((BikeDto)dto[1]).Brand.ShouldBe("BMX");
}
/// <summary>
/// https://github.com/MapsterMapper/Mapster/issues/801
/// </summary>
[TestMethod]
public void CompileProjection_Including_Derived_Class()
{
TypeAdapterConfig<PocoA801, DtoA801>.NewConfig()
.Include<PocoDerived801, DtoDerived801>()
.CompileProjection();
}
public abstract class PocoA801
{
public int Id { get; set; }
}
public class PocoDerived801 : PocoA801
{
public int DerivedVal { get; set; }
}
public abstract class DtoA801
{
public int Id { get; set; }
}
public class DtoDerived801 : DtoA801
{
public int DerivedVal { get; set; }
}
#region test classes
public abstract class Vehicle
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Car : Vehicle
{
public string Make { get; set; }
public string ChassiNumber { get; set; }
}
public class Bike : Vehicle
{
public string Brand { get; set; }
}
public abstract class VehicleDto
{
public int Id { get; set; }
public string Name { get; set; }
}
public class CarDto : VehicleDto
{
public string Make { get; set; }
public string ChassiNumber { get; set; }
}
public class BikeDto : VehicleDto
{
public string Brand { get; set; }
}
#endregion
}
}