forked from AutoMapper/AutoMapper.Archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalMapping.cs
More file actions
289 lines (243 loc) · 8.86 KB
/
Copy pathConditionalMapping.cs
File metadata and controls
289 lines (243 loc) · 8.86 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
namespace AutoMapper.UnitTests.ConditionalMapping;
public class When_adding_a_condition_for_all_members : AutoMapperSpecBase
{
Source _source = new Source { Value = 3 };
Destination _destination = new Destination { Value = 7 };
class Source
{
public int Value { get; set; }
}
class Destination
{
public int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>().ForAllMembers(o => o.Condition((source, destination, sourceProperty, destinationProperty) =>
{
source.ShouldBeSameAs(_source);
destination.ShouldBeSameAs(_destination);
((int)sourceProperty).ShouldBe(3);
((int)destinationProperty).ShouldBe(7);
return true;
}));
});
[Fact]
public void Should_work() => Mapper.Map(_source, _destination);
}
public class When_ignoring_all_properties_with_an_inaccessible_setter_and_explicitly_implemented_member : AutoMapperSpecBase
{
protected override MapperConfiguration CreateConfiguration() => new(c => c.CreateMap<SourceClass, DestinationClass>().IgnoreAllPropertiesWithAnInaccessibleSetter());
interface Interface
{
int Value { get; }
}
class SourceClass
{
public int PublicProperty { get; set; }
}
class DestinationClass : Interface
{
int Interface.Value { get { return 123; } }
public int PrivateProperty { get; private set; }
public int PublicProperty { get; set; }
}
[Fact]
public void Validate() => AssertConfigurationIsValid();
}
public class When_configuring_a_member_to_skip_based_on_the_property_value : AutoMapperSpecBase
{
public class Source
{
public int Value { get; set; }
}
public class Destination
{
public int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Value, opt => opt.Condition(src => src.Value > 0));
});
[Fact]
public void Should_skip_the_mapping_when_the_condition_is_true()
{
var destination = Mapper.Map<Source, Destination>(new Source {Value = -1});
destination.Value.ShouldBe(0);
}
[Fact]
public void Should_execute_the_mapping_when_the_condition_is_false()
{
var destination = Mapper.Map<Source, Destination>(new Source { Value = 7 });
destination.Value.ShouldBe(7);
}
}
public class When_configuring_a_member_to_skip_based_on_the_property_value_with_custom_mapping : AutoMapperSpecBase
{
public class Source
{
public int Value { get; set; }
}
public class Destination
{
public int Value { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.Value, opt =>
{
opt.Condition(src => src.Value > 0);
opt.MapFrom(src => 10);
});
});
[Fact]
public void Should_skip_the_mapping_when_the_condition_is_true()
{
var destination = Mapper.Map<Source, Destination>(new Source { Value = -1 });
destination.Value.ShouldBe(0);
}
[Fact]
public void Should_execute_the_mapping_when_the_condition_is_false()
{
Mapper.Map<Source, Destination>(new Source { Value = 7 }).Value.ShouldBe(10);
}
}
public class When_configuring_a_map_to_ignore_all_properties_with_an_inaccessible_setter : AutoMapperSpecBase
{
private Destination _destination;
public class Source
{
public int Id { get; set; }
public string Title { get; set; }
public string CodeName { get; set; }
public string Nickname { get; set; }
public string ScreenName { get; set; }
}
public class Destination
{
private double _height;
public int Id { get; set; }
public virtual string Name { get; protected set; }
public string Title { get; internal set; }
public string CodeName { get; private set; }
public string Nickname { get; private set; }
public string ScreenName { get; private set; }
public int Age { get; private set; }
public double Height
{
get { return _height; }
}
public Destination()
{
_height = 60;
}
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.ScreenName, opt => opt.MapFrom(src => src.ScreenName))
.IgnoreAllPropertiesWithAnInaccessibleSetter()
.ForMember(dest => dest.Nickname, opt => opt.MapFrom(src => src.Nickname));
});
protected override void Because_of()
{
_destination = Mapper.Map<Source, Destination>(new Source { Id = 5, CodeName = "007", Nickname = "Jimmy", ScreenName = "jbogard" });
}
[Fact]
public void Should_consider_the_configuration_valid_even_if_some_properties_with_an_inaccessible_setter_are_unmapped()
{
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(AssertConfigurationIsValid);
}
[Fact]
public void Should_map_a_property_with_an_inaccessible_setter_if_a_specific_mapping_is_configured_after_the_ignore_method()
{
_destination.Nickname.ShouldBe("Jimmy");
}
[Fact]
public void Should_not_map_a_property_with_an_inaccessible_setter_if_no_specific_mapping_is_configured_even_though_name_and_type_match()
{
_destination.CodeName.ShouldBeNull();
}
[Fact]
public void Should_not_map_a_property_with_no_public_setter_if_a_specific_mapping_is_configured_before_the_ignore_method()
{
_destination.ScreenName.ShouldBeNull();
}
}
public class When_configuring_a_reverse_map_to_ignore_all_source_properties_with_an_inaccessible_setter : AutoMapperSpecBase
{
private Destination _destination;
private Source _source;
public class Source
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Force { get; set; }
public string ReverseForce { get; private set; }
public string Respect { get; private set; }
public int Foo { get; private set; }
public int Bar { get; protected set; }
public void Initialize()
{
ReverseForce = "You With";
Respect = "R-E-S-P-E-C-T";
}
}
public class Destination
{
public string Name { get; set; }
public int Age { get; set; }
public bool IsVisible { get; set; }
public string Force { get; private set; }
public string ReverseForce { get; set; }
public string Respect { get; set; }
public int Foz { get; private set; }
public int Baz { get; protected set; }
}
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<Source, Destination>()
.IgnoreAllPropertiesWithAnInaccessibleSetter()
.ForMember(dest => dest.IsVisible, opt => opt.Ignore())
.ForMember(dest => dest.Force, opt => opt.MapFrom(src => src.Force))
.ReverseMap()
.IgnoreAllSourcePropertiesWithAnInaccessibleSetter()
.ForMember(dest => dest.ReverseForce, opt => opt.MapFrom(src => src.ReverseForce))
.ForSourceMember(dest => dest.IsVisible, opt => opt.DoNotValidate());
});
protected override void Because_of()
{
var source = new Source { Id = 5, Name = "Bob", Age = 35, Force = "With You" };
source.Initialize();
_destination = Mapper.Map<Source, Destination>(source);
_source = Mapper.Map<Destination, Source>(_destination);
}
[Fact]
public void Should_consider_the_configuration_valid_even_if_some_properties_with_an_inaccessible_setter_are_unmapped()
{
typeof(AutoMapperConfigurationException).ShouldNotBeThrownBy(AssertConfigurationIsValid);
}
[Fact]
public void Should_forward_and_reverse_map_a_property_that_is_accessible_on_both_source_and_destination()
{
_source.Name.ShouldBe("Bob");
}
[Fact]
public void Should_forward_and_reverse_map_an_inaccessible_destination_property_if_a_mapping_is_defined()
{
_source.Force.ShouldBe("With You");
}
[Fact]
public void Should_forward_and_reverse_map_an_inaccessible_source_property_if_a_mapping_is_defined()
{
_source.ReverseForce.ShouldBe("You With");
}
[Fact]
public void Should_forward_and_reverse_map_an_inaccessible_source_property_even_if_a_mapping_is_not_defined()
{
_source.Respect.ShouldBe("R-E-S-P-E-C-T"); // justification: if the mapping works one way, it should work in reverse
}
}