forked from AutoMapper/AutoMapper.Archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildExecutionPlan.cs
More file actions
174 lines (174 loc) · 5.43 KB
/
Copy pathBuildExecutionPlan.cs
File metadata and controls
174 lines (174 loc) · 5.43 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
namespace AutoMapper.UnitTests;
public class BuildExecutionPlan : AutoMapperSpecBase
{
Model _source;
Dto _destination;
public class Model
{
public Guid? Id { get; set; }
public Guid? FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
public int? IntValue { get; set; }
}
public class Dto
{
public Guid? Id { get; set; }
public string FooId { get; set; }
public string FullDescription { get; set; }
public string ShortDescription { get; set; }
public DateTime Date { get; set; }
public int IntValue { get; set; }
public string CompanyName { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.CreateMap<Model, Dto>().ForMember(d => d.CompanyName, o => o.Ignore());
});
protected override void Because_of()
{
_source = new Model
{
Id = Guid.NewGuid(),
FooId = Guid.NewGuid(),
ShortDescription = "Yoyodyne Foo",
FullDescription = "Deluxe Foo Manufactured by Yoyodyne, Inc.",
Date = DateTime.Now,
IntValue = 13,
};
var plan = Configuration.BuildExecutionPlan(typeof(Model), typeof(Dto));
_destination = ((Func<Model, Dto, ResolutionContext, Dto>)plan.Compile())(_source, null, null);
}
[Fact]
public void Should_build_the_execution_plan()
{
_destination.Id.ShouldBe(_source.Id);
_destination.FooId.ShouldBe(_source.FooId.ToString());
_destination.ShortDescription.ShouldBe(_source.ShortDescription);
_destination.FullDescription.ShouldBe(_source.FullDescription);
_destination.Date.ShouldBe(_source.Date);
_destination.IntValue.ShouldBe(_source.IntValue.Value);
}
}
public class When_reusing_the_execution_plan_inner_map : AutoMapperSpecBase
{
class Source
{
public Inner Inner { get; set; }
}
class Destination
{
public Inner Inner { get; set; }
}
class Inner { }
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.AllowNullDestinationValues = false;
c.CreateMap<Inner, Inner>();
c.CreateMap<Source, Destination>().ForAllMembers(o =>
{
o.AllowNull();
o.MapAtRuntime();
});
});
[Fact]
public void Should_consider_per_member_settings()
{
Mapper.Map<Inner, Inner>(null).ShouldNotBeNull();
var destination = Map<Destination>(new Source());
destination.Inner.ShouldBeNull();
}
}
public class AllowNullWithMapAtRuntime : AutoMapperSpecBase
{
class Source
{
public Inner Inner { get; set; }
}
class Destination
{
public Inner Inner { get; set; }
}
class Inner { }
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.AllowNullDestinationValues = false;
c.CreateMap<Inner, Inner>();
c.CreateMap<Source, Destination>().ForAllMembers(o =>
{
o.AllowNull();
o.MapAtRuntime();
});
});
[Fact]
public void Should_consider_per_member_settings()
{
var destination = Map<Destination>(new Source());
destination.Inner.ShouldBeNull();
}
}
public class When_reusing_the_execution_plan : AutoMapperSpecBase
{
class Source
{
public int[] Ints { get; set; }
public string String { get; set; }
}
class Destination
{
public int[] Ints { get; set; }
public string String { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.AllowNullDestinationValues = false;
c.CreateMap<Source, Destination>().ForAllMembers(o =>
{
o.AllowNull();
o.MapAtRuntime();
});
});
[Fact]
public void Should_consider_per_member_settings()
{
Mapper.Map<string, string>(null).Length.ShouldBe(0);
Mapper.Map<int[], int[]>(null).Length.ShouldBe(0);
var destination = Map<Destination>(new Source());
destination.Ints.ShouldBeNull();
destination.String.ShouldBeNull();
}
}
public class When_reusing_the_execution_plan_existing_destination : AutoMapperSpecBase
{
class Source
{
public int[] Ints { get; set; }
}
class OtherSource
{
public int[] Ints { get; set; }
}
class Destination
{
public ICollection<int> Ints { get; set; } = new HashSet<int>();
}
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.CreateMap<OtherSource, Destination>().ForAllMembers(o => o.MapAtRuntime());
c.CreateMap<Source, Destination>().ForAllMembers(o =>
{
o.UseDestinationValue();
o.MapAtRuntime();
});
});
[Fact]
public void Should_consider_per_member_settings()
{
var ints = new[] { 1, 1, 1 };
var destination = Map<Destination>(new OtherSource { Ints = ints });
destination.Ints.ShouldBe(ints);
destination = Map<Destination>(new Source { Ints = ints });
destination.Ints.ShouldBe(new[] { 1 });
}
}