|
1 | 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
2 | 2 | using Shouldly; |
3 | | -using System; |
4 | | -using System.Collections.Generic; |
5 | | -using System.Linq; |
6 | | -using System.Text; |
7 | | -using System.Threading.Tasks; |
8 | 3 |
|
9 | 4 | namespace Mapster.Tests |
10 | 5 | { |
@@ -36,6 +31,60 @@ public void Setting_From_OpenGeneric_Has_No_SideEffect() |
36 | 31 | var cCopy = c.Adapt<C>(config); |
37 | 32 | } |
38 | 33 |
|
| 34 | + [TestMethod] |
| 35 | + public void MapOpenGenericsUseInherits() |
| 36 | + { |
| 37 | + TypeAdapterConfig.GlobalSettings |
| 38 | + .ForType(typeof(GenericPoco<>), typeof(GenericDto<>)) |
| 39 | + .Map("value", "Value"); |
| 40 | + |
| 41 | + TypeAdapterConfig.GlobalSettings |
| 42 | + .ForType(typeof(DerivedPoco<>), typeof(DerivedDto<>)) |
| 43 | + .Map("derivedValue", "DerivedValue") |
| 44 | + .Inherits(typeof(GenericPoco<>), typeof(GenericDto<>)); |
| 45 | + |
| 46 | + var poco = new DerivedPoco<int> { Value = 123 , DerivedValue = 42 }; |
| 47 | + var dto = poco.Adapt<DerivedDto<int>>(); |
| 48 | + dto.value.ShouldBe(poco.Value); |
| 49 | + dto.derivedValue.ShouldBe(poco.DerivedValue); |
| 50 | + } |
| 51 | + |
| 52 | + [TestMethod] |
| 53 | + public void MapOpenGenericsUseInclude() |
| 54 | + { |
| 55 | + TypeAdapterConfig.GlobalSettings.Clear(); |
| 56 | + |
| 57 | + TypeAdapterConfig.GlobalSettings |
| 58 | + .ForType(typeof(DerivedPoco<>), typeof(DerivedDto<>)) |
| 59 | + .Map("derivedValue", "DerivedValue"); |
| 60 | + |
| 61 | + TypeAdapterConfig.GlobalSettings |
| 62 | + .ForType(typeof(GenericPoco<>), typeof(GenericDto<>)) |
| 63 | + .Map("value", "Value"); |
| 64 | + |
| 65 | + TypeAdapterConfig.GlobalSettings |
| 66 | + .ForType(typeof(GenericPoco<>), typeof(GenericDto<>)) |
| 67 | + .Include(typeof(DerivedPoco<>), typeof(DerivedDto<>)); |
| 68 | + |
| 69 | + var poco = new DerivedPoco<int> { Value = 123, DerivedValue = 42 }; |
| 70 | + var dto = poco.Adapt(typeof(GenericPoco<>), typeof(GenericDto<>)); |
| 71 | + |
| 72 | + dto.ShouldBeOfType<DerivedDto<int>>(); |
| 73 | + |
| 74 | + ((DerivedDto<int>)dto).value.ShouldBe(poco.Value); |
| 75 | + ((DerivedDto<int>)dto).derivedValue.ShouldBe(poco.DerivedValue); |
| 76 | + } |
| 77 | + |
| 78 | + public class DerivedPoco<T> : GenericPoco<T> |
| 79 | + { |
| 80 | + public T DerivedValue { get; set; } |
| 81 | + } |
| 82 | + |
| 83 | + public class DerivedDto<T> : GenericDto<T> |
| 84 | + { |
| 85 | + public T derivedValue { get; set; } |
| 86 | + } |
| 87 | + |
39 | 88 | public class GenericPoco<T> |
40 | 89 | { |
41 | 90 | public T Value { get; set; } |
|
0 commit comments