-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathEnumValueMappingToException.cs
More file actions
72 lines (63 loc) · 2.15 KB
/
EnumValueMappingToException.cs
File metadata and controls
72 lines (63 loc) · 2.15 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
using System;
using AutoMapper.Extensions.EnumMapping.Tests.Internal;
using Shouldly;
using Xunit;
namespace AutoMapper.Extensions.EnumMapping.Tests;
public class EnumValueMappingToException
{
public class MapToExceptionNotAffectOtherMappings : AutoMapperSpecBase
{
Destination _result;
public enum Source { A, B}
public enum Destination { A = 2 }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByName()
.MapValue(Source.A, Destination.A)
.MapException(Source.B, () => new NotSupportedException($"B is not valid value"))
);
}, _loggerFactory);
protected override void Because_of()
{
_result = Mapper.Map<Source, Destination>(Source.A);
}
[Fact]
public void Should_map_using_custom_map()
{
_result.ShouldBe(Destination.A);
}
}
public class MapToExceptionThrowException : AutoMapperSpecBase
{
Exception _result;
public enum Source { A, B}
public enum Destination { A = 2 }
protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg =>
{
cfg.EnableEnumMappingValidation();
cfg.CreateMap<Source, Destination>()
.ConvertUsingEnumMapping(opt => opt.MapByName()
.MapValue(Source.A, Destination.A)
.MapException(Source.B, () => new NotSupportedException($"B is not valid value"))
);
}, _loggerFactory);
protected override void Because_of()
{
try
{
Mapper.Map<Source, Destination>(Source.B);
}
catch (Exception ex)
{
_result = ex;
}
}
[Fact]
public void Should_map_using_custom_map()
{
_result.ShouldBeOfType<NotSupportedException>();
}
}
}