-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathWhenUsingMapWithOverrideTypesSettings.cs
More file actions
89 lines (71 loc) · 3.05 KB
/
Copy pathWhenUsingMapWithOverrideTypesSettings.cs
File metadata and controls
89 lines (71 loc) · 3.05 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
namespace Mapster.Tests
{
[TestClass]
public class WhenUsingMapWithOverrideTypesSettings
{
[TestMethod]
public void OverrideDestinationTramsformIsWorked()
{
var config = new TypeAdapterConfig();
config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);
config
.NewConfig<CollectionPocoOverride, CollectionDtoOverride>()
.MapWithTypeSettingsOverride(src => src.Children, dest => dest.Children,
cfg =>
{
cfg.SkipSettings(x => x.DestinationTransforms);
})
.MapWithTypeSettingsOverride(src => src.Array, dest => dest.Array,
cfg =>
{
cfg
.ReConfigurate()
.MapWith(x => x ?? new[] { 42 });
});
var source = new CollectionPocoOverride();
var destination = source.Adapt<CollectionDtoOverride>(config);
destination.MultiDimentionalArray.Length.ShouldBe(0);
destination.ChildDict.Count.ShouldBe(0);
destination.Set.Count.ShouldBe(0);
destination.Children.ShouldBeNull(); // Destination Transforms from global context settings is skipped for this property
destination.Array[0].ShouldBe(42); // Custom converter for types is worked, Destination Transforms is not achievable because the custom converter never returns null
var destWithNotTypesSettingOverride = new CollectionPocoOverride().Adapt<CollectionDtoWithArray>(config);
// Destination Transforms correct work from other mapping types
destWithNotTypesSettingOverride.Array.Length.ShouldBe(0);
}
#region TestClasses
class CollectionPocoWithArray
{
public int[] Array { get; set; }
}
class CollectionDtoWithArray
{
public int[] Array { get; set; }
}
class CollectionPocoOverride
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<ChildPoco> Children { get; set; }
public int[] Array { get; set; }
public double[,] MultiDimentionalArray { get; set; }
public Dictionary<string, ChildPoco> ChildDict { get; set; }
public HashSet<string> Set { get; set; }
}
class CollectionDtoOverride
{
public Guid Id { get; set; }
public string Name { get; set; }
public IReadOnlyList<ChildDto> Children { get; internal set; }
public int[] Array { get; set; }
public double[,] MultiDimentionalArray { get; set; }
public IReadOnlyDictionary<string, ChildDto> ChildDict { get; set; }
public ISet<string> Set { get; set; }
}
#endregion TestClasses
}
}