-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathReverseCustomEnumValueMappingByValue.cs
More file actions
204 lines (180 loc) · 8.65 KB
/
ReverseCustomEnumValueMappingByValue.cs
File metadata and controls
204 lines (180 loc) · 8.65 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
using System;
using System.Collections.Generic;
using AutoMapper.Extensions.EnumMapping.Tests.Internal;
using Shouldly;
using Xunit;
namespace AutoMapper.Extensions.EnumMapping.Tests
{
public class ReverseCustomEnumValueMappingByValue
{
public class MissingDestinationValue : AutoMapperSpecBase
{
readonly List<Source> _results = new List<Source>();
public enum Source { Default, Foo, OnlyInSource }
public enum Destination { Default, Foo }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue()
.MapValue(Source.OnlyInSource, Destination.Foo))
.ReverseMap();
}, _loggerFactory);
protected override void Because_of()
{
_results.Add(Mapper.Map<Destination, Source>(Destination.Default));
_results.Add(Mapper.Map<Destination, Source>(Destination.Foo));
}
[Fact]
public void Should_map_using_custom_map()
{
_results[0].ShouldBe(Source.Default);
_results[1].ShouldBe(Source.Foo);
}
}
public class MissingSourceValue : NonValidatingSpecBase
{
public enum Source { Default, Foo }
public enum Destination { Default, Foo, OnlyInDestination }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue())
.ReverseMap();
}, _loggerFactory);
[Fact]
public void Should_fail_validation() =>
new Action(() => Configuration.AssertConfigurationIsValid()).ShouldThrowException<AutoMapperConfigurationException>(
ex => ex.Message.ShouldBe(
$@"Missing enum mapping from {typeof(Destination).FullName} to {typeof(Source).FullName} based on Value{Environment.NewLine}The following source values are not mapped:{Environment.NewLine} - OnlyInDestination{Environment.NewLine}"));
}
public class OnlyInSourceValueIsMappedToOnlyInDestinationValue : AutoMapperSpecBase
{
readonly List<Source> _results = new List<Source>();
public enum Source { Default, OnlyInSource }
public enum Destination { Default, OnlyInDestination }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue()
.MapValue(Source.OnlyInSource, Destination.OnlyInDestination))
.ReverseMap();
}, _loggerFactory);
protected override void Because_of()
{
_results.Add(Mapper.Map<Destination, Source>(Destination.Default));
_results.Add(Mapper.Map<Destination, Source>(Destination.OnlyInDestination));
}
[Fact]
public void Should_map_using_custom_map()
{
_results[0].ShouldBe(Source.Default);
_results[1].ShouldBe(Source.OnlyInSource);
}
}
public class OnlyInSourceValueIsMappedToDestinationValueButDestinationValueHasAlsoSameSourceValueMapping : AutoMapperSpecBase
{
readonly List<Source> _results = new List<Source>();
public enum Source { A, B, C, D, InSourceAndDestionation, OnyInSource }
public enum Destination { A, B, C, D, InSourceAndDestionation }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue()
.MapValue(Source.A, Destination.B)
.MapValue(Source.D, Destination.InSourceAndDestionation)
.MapValue(Source.InSourceAndDestionation, Destination.D)
.MapValue(Source.OnyInSource, Destination.InSourceAndDestionation))
.ReverseMap();
}, _loggerFactory);
protected override void Because_of()
{
_results.Add(Mapper.Map<Destination, Source>(Destination.A));
_results.Add(Mapper.Map<Destination, Source>(Destination.B));
_results.Add(Mapper.Map<Destination, Source>(Destination.C));
_results.Add(Mapper.Map<Destination, Source>(Destination.D));
_results.Add(Mapper.Map<Destination, Source>(Destination.InSourceAndDestionation));
}
[Fact]
public void Should_map_using_custom_map()
{
_results[0].ShouldBe(Source.A);
_results[1].ShouldBe(Source.B);
_results[2].ShouldBe(Source.C);
_results[3].ShouldBe(Source.InSourceAndDestionation);
_results[4].ShouldBe(Source.D);
}
}
public class AllSourceValuesAreMappedToOtherDestinationValues : AutoMapperSpecBase
{
readonly List<Source> _results = new List<Source>();
public enum Source { A, B, C, D, E, F }
public enum Destination { A, B, C, D, E, F }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue()
.MapValue(Source.F, Destination.A)
.MapValue(Source.A, Destination.B)
.MapValue(Source.B, Destination.C)
.MapValue(Source.C, Destination.D)
.MapValue(Source.D, Destination.E)
.MapValue(Source.E, Destination.A)
)
.ReverseMap();
}, _loggerFactory);
protected override void Because_of()
{
_results.Add(Mapper.Map<Destination, Source>(Destination.A));
_results.Add(Mapper.Map<Destination, Source>(Destination.B));
_results.Add(Mapper.Map<Destination, Source>(Destination.C));
_results.Add(Mapper.Map<Destination, Source>(Destination.D));
_results.Add(Mapper.Map<Destination, Source>(Destination.E));
_results.Add(Mapper.Map<Destination, Source>(Destination.F));
}
[Fact]
public void Should_map_using_custom_map()
{
_results[0].ShouldBe(Source.E);
_results[1].ShouldBe(Source.A);
_results[2].ShouldBe(Source.B);
_results[3].ShouldBe(Source.C);
_results[4].ShouldBe(Source.D);
_results[5].ShouldBe(Source.F);
}
}
public class AllSourceValuesAreMappedToOneDestinationValue : AutoMapperSpecBase
{
Source _result;
public enum Source { A, B, C, D, E, F }
public enum Destination { C = 2 }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByValue()
.MapValue(Source.A, Destination.C)
.MapValue(Source.B, Destination.C)
.MapValue(Source.C, Destination.C)
.MapValue(Source.D, Destination.C)
.MapValue(Source.E, Destination.C)
.MapValue(Source.F, Destination.C)
)
.ReverseMap();
}, _loggerFactory);
protected override void Because_of()
{
_result = Mapper.Map<Destination, Source>(Destination.C);
}
[Fact]
public void Should_map_using_custom_map()
{
_result.ShouldBe(Source.C);
}
}
}
}