|
| 1 | +using System; |
| 2 | +using AutoMapper.Extensions.EnumMapping.Tests.Internal; |
| 3 | +using Shouldly; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace AutoMapper.Extensions.EnumMapping.Tests |
| 7 | +{ |
| 8 | + public class ReverseEnumValueMappingByCustom |
| 9 | + { |
| 10 | + public class Valid : AutoMapperSpecBase |
| 11 | + { |
| 12 | + Destination _result; |
| 13 | + public enum Source { Default, Foo, Bar } |
| 14 | + public enum Destination { Default, Bar, Foo } |
| 15 | + |
| 16 | + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => |
| 17 | + { |
| 18 | + cfg.EnableEnumMappingValidation(); |
| 19 | + cfg.CreateMap<Source, Destination>() |
| 20 | + .ConvertUsingEnumMapping(opt => opt |
| 21 | + .MapByCustom() |
| 22 | + .MapValue(Source.Default, Destination.Default) |
| 23 | + .MapValue(Source.Foo, Destination.Foo) |
| 24 | + .MapValue(Source.Bar, Destination.Bar) |
| 25 | + ) |
| 26 | + .ReverseMap(); |
| 27 | + }); |
| 28 | + |
| 29 | + protected override void Because_of() |
| 30 | + { |
| 31 | + _result = Mapper.Map<Source, Destination>(Source.Bar); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void Should_map_enum_by_custom() |
| 36 | + { |
| 37 | + _result.ShouldBe(Destination.Bar); |
| 38 | + ((int)_result).ShouldBe((int)Source.Foo); |
| 39 | + |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public class ValidCustomMapping : AutoMapperSpecBase |
| 44 | + { |
| 45 | + Destination _result; |
| 46 | + public enum Source { Default, Bar } |
| 47 | + public enum Destination { Default, Bar } |
| 48 | + |
| 49 | + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => |
| 50 | + { |
| 51 | + cfg.EnableEnumMappingValidation(); |
| 52 | + cfg.CreateMap<Source, Destination>() |
| 53 | + .ConvertUsingEnumMapping(opt => opt |
| 54 | + .MapByCustom() |
| 55 | + .MapValue(Source.Default, Destination.Default) |
| 56 | + .MapValue(Source.Bar, Destination.Bar) |
| 57 | + ) |
| 58 | + .ReverseMap(); |
| 59 | + }); |
| 60 | + |
| 61 | + protected override void Because_of() |
| 62 | + { |
| 63 | + _result = Mapper.Map<Source, Destination>(Source.Bar); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void Should_map_using_custom_map() |
| 68 | + { |
| 69 | + _result.ShouldBe(Destination.Bar); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public class ValidationErrors : NonValidatingSpecBase |
| 74 | + { |
| 75 | + public enum Source { Default, Foo, Bar } |
| 76 | + public enum Destination { Default, Bar } |
| 77 | + |
| 78 | + protected override MapperConfiguration Configuration => new MapperConfiguration(cfg => |
| 79 | + { |
| 80 | + cfg.EnableEnumMappingValidation(); |
| 81 | + cfg.CreateMap<Source, Destination>() |
| 82 | + .ConvertUsingEnumMapping(opt => opt |
| 83 | + .MapByCustom() |
| 84 | + .MapValue(Source.Default, Destination.Default) |
| 85 | + .MapValue(Source.Bar, Destination.Bar) |
| 86 | + ) |
| 87 | + .ReverseMap(); |
| 88 | + }); |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void Should_fail_validation() => |
| 92 | + new Action(() => Configuration.AssertConfigurationIsValid()).ShouldThrowException<AutoMapperConfigurationException>( |
| 93 | + ex => ex.Message.ShouldBe( |
| 94 | + $@"Missing enum mapping from {typeof(Source).FullName} to {typeof(Destination).FullName} based on Custom{Environment.NewLine}The following source values are not mapped:{Environment.NewLine} - Foo{Environment.NewLine}")); |
| 95 | + } |
| 96 | + |
| 97 | + public class CustomMappingWithValidationErrors : NonValidatingSpecBase |
| 98 | + { |
| 99 | + public enum Source { Default, Foo, Bar, Error } |
| 100 | + public enum Destination { Default, Bar } |
| 101 | + |
| 102 | + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => |
| 103 | + { |
| 104 | + cfg.EnableEnumMappingValidation(); |
| 105 | + cfg.CreateMap<Source, Destination>() |
| 106 | + .ConvertUsingEnumMapping(opt => opt |
| 107 | + .MapByCustom() |
| 108 | + .MapValue(Source.Default, Destination.Default) |
| 109 | + .MapException(Source.Foo, () => new NotSupportedException($"Foo is not valid value")) |
| 110 | + .MapValue(Source.Bar, Destination.Bar)) |
| 111 | + .ReverseMap(); |
| 112 | + }); |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void Should_fail_validation() => |
| 116 | + new Action(() => Configuration.AssertConfigurationIsValid()).ShouldThrowException<AutoMapperConfigurationException>( |
| 117 | + ex => ex.Message.ShouldBe( |
| 118 | + $@"Missing enum mapping from {typeof(Source).FullName} to {typeof(Destination).FullName} based on Custom{Environment.NewLine}The following source values are not mapped:{Environment.NewLine} - Error{Environment.NewLine}")); |
| 119 | + } |
| 120 | + |
| 121 | + public class ValidCustomReverseMapping : AutoMapperSpecBase |
| 122 | + { |
| 123 | + Source _resultDefault; |
| 124 | + Source _resultFoo; |
| 125 | + Source _resultBar; |
| 126 | + public enum Source { Default, Bar } |
| 127 | + public enum Destination { Default, Foo, Bar } |
| 128 | + |
| 129 | + protected override MapperConfiguration Configuration { get; } = new MapperConfiguration(cfg => |
| 130 | + { |
| 131 | + cfg.EnableEnumMappingValidation(); |
| 132 | + cfg.CreateMap<Source, Destination>() |
| 133 | + .ConvertUsingEnumMapping(opt => opt |
| 134 | + .MapByCustom() |
| 135 | + .MapValue(Source.Default, Destination.Default) |
| 136 | + .MapValue(Source.Bar, Destination.Bar)) |
| 137 | + .ReverseMap(optr => optr.MapByCustom().MapValue(Destination.Foo, Source.Bar)); |
| 138 | + }); |
| 139 | + |
| 140 | + protected override void Because_of() |
| 141 | + { |
| 142 | + _resultDefault = Mapper.Map<Source>(Destination.Default); |
| 143 | + _resultFoo = Mapper.Map<Source>(Destination.Foo); |
| 144 | + _resultBar = Mapper.Map<Source>(Destination.Bar); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact] |
| 148 | + public void Should_map_using_reverse_custom_map() |
| 149 | + { |
| 150 | + _resultDefault.ShouldBe(Source.Default); |
| 151 | + _resultFoo.ShouldBe(Source.Bar); |
| 152 | + _resultBar.ShouldBe(Source.Bar); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments