|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 8 | +{ |
| 9 | + public class CanMapMemberFromTypeBinaryExpression |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public void Can_map_using_type_binary_expression_to_test_the_parameter_expression() |
| 13 | + { |
| 14 | + //arrange |
| 15 | + var config = new MapperConfiguration(cfg => |
| 16 | + { |
| 17 | + cfg.AddExpressionMapping(); |
| 18 | + |
| 19 | + cfg.CreateMap<Shape, ShapeDto>() |
| 20 | + .ForMember(dto => dto.ShapeType, o => o.MapFrom(s => s is Triangle ? ShapeType.Triangle : s is Circle ? ShapeType.Circle : ShapeType.Unknown)) |
| 21 | + .ForMember(dto => dto.DynamicProperties, o => o.Ignore()); |
| 22 | + }); |
| 23 | + |
| 24 | + var mapper = config.CreateMapper(); |
| 25 | + Expression<Func<ShapeDto, bool>> where = x => x.ShapeType == ShapeType.Circle; |
| 26 | + |
| 27 | + //act |
| 28 | + Expression<Func<Shape, bool>> whereMapped = mapper.MapExpression<Expression<Func<Shape, bool>>>(where); |
| 29 | + var list = new List<Shape> { new Circle(), new Triangle() }.AsQueryable().Where(whereMapped).ToList(); |
| 30 | + |
| 31 | + //assert |
| 32 | + Assert.Single(list); |
| 33 | + Assert.Equal |
| 34 | + ( |
| 35 | + "x => (Convert(IIF((x Is Triangle), Triangle, IIF((x Is Circle), Circle, Unknown)), Int32) == 2)", |
| 36 | + whereMapped.ToString() |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Can_map_using_type_binary_expression_to_test_a_member_expression() |
| 42 | + { |
| 43 | + //arrange |
| 44 | + var config = new MapperConfiguration(cfg => |
| 45 | + { |
| 46 | + cfg.AddExpressionMapping(); |
| 47 | + |
| 48 | + cfg.CreateMap<ShapeHolder, ShapeHolderDto>(); |
| 49 | + cfg.CreateMap<Shape, ShapeDto>() |
| 50 | + .ForMember(dto => dto.ShapeType, o => o.MapFrom(s => s is Triangle ? ShapeType.Triangle : s is Circle ? ShapeType.Circle : ShapeType.Unknown)) |
| 51 | + .ForMember(dto => dto.DynamicProperties, o => o.Ignore()); |
| 52 | + }); |
| 53 | + |
| 54 | + var mapper = config.CreateMapper(); |
| 55 | + Expression<Func<ShapeHolderDto, bool>> where = x => x.Shape.ShapeType == ShapeType.Circle; |
| 56 | + |
| 57 | + //act |
| 58 | + Expression<Func<ShapeHolder, bool>> whereMapped = mapper.MapExpression<Expression<Func<ShapeHolder, bool>>>(where); |
| 59 | + var list = new List<ShapeHolder> |
| 60 | + { |
| 61 | + new ShapeHolder { Shape = new Circle() }, |
| 62 | + new ShapeHolder { Shape = new Triangle() } |
| 63 | + } |
| 64 | + .AsQueryable() |
| 65 | + .Where(whereMapped).ToList(); |
| 66 | + |
| 67 | + //assert |
| 68 | + Assert.Single(list); |
| 69 | + Assert.Equal |
| 70 | + ( |
| 71 | + "x => (Convert(IIF((x.Shape Is Triangle), Triangle, IIF((x.Shape Is Circle), Circle, Unknown)), Int32) == 2)", |
| 72 | + whereMapped.ToString() |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Can_map_using_instance_method_call_to_test_the_parameter_expression() |
| 78 | + { |
| 79 | + //arrange |
| 80 | + var config = new MapperConfiguration(cfg => |
| 81 | + { |
| 82 | + cfg.AddExpressionMapping(); |
| 83 | + |
| 84 | + cfg.CreateMap<Shape, ShapeDto>() |
| 85 | + .ForMember(dto => dto.ShapeType, o => o.MapFrom(s => s.GetType() == typeof(Triangle) ? ShapeType.Triangle : s.GetType() == typeof(Circle) ? ShapeType.Circle : ShapeType.Unknown)) |
| 86 | + .ForMember(dto => dto.DynamicProperties, o => o.Ignore()); |
| 87 | + }); |
| 88 | + |
| 89 | + var mapper = config.CreateMapper(); |
| 90 | + Expression<Func<ShapeDto, bool>> where = x => x.ShapeType == ShapeType.Circle; |
| 91 | + |
| 92 | + //act |
| 93 | + Expression<Func<Shape, bool>> whereMapped = mapper.MapExpression<Expression<Func<Shape, bool>>>(where); |
| 94 | + var list = new List<Shape> { new Circle(), new Triangle() }.AsQueryable().Where(whereMapped).ToList(); |
| 95 | + |
| 96 | + //assert |
| 97 | + Assert.Single(list); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void Can_map_using_static_method_call_to_test_the_parameter_expression() |
| 102 | + { |
| 103 | + //arrange |
| 104 | + var config = new MapperConfiguration(cfg => |
| 105 | + { |
| 106 | + cfg.AddExpressionMapping(); |
| 107 | + |
| 108 | + cfg.CreateMap<Shape, ShapeDto>() |
| 109 | + .ForMember(dto => dto.HasMany, o => o.MapFrom(s => s.HasMessages())); |
| 110 | + }); |
| 111 | + |
| 112 | + var mapper = config.CreateMapper(); |
| 113 | + Expression<Func<ShapeDto, bool>> where = x => x.HasMany; |
| 114 | + |
| 115 | + //act |
| 116 | + Expression<Func<Shape, bool>> whereMapped = mapper.MapExpression<Expression<Func<Shape, bool>>>(where); |
| 117 | + var list = new List<Shape> { new Circle() { Messages = new List<string> { "" } }, new Triangle() { Messages = new List<string>() } }.AsQueryable().Where(whereMapped).ToList(); |
| 118 | + |
| 119 | + //assert |
| 120 | + Assert.Single(list); |
| 121 | + Assert.Equal |
| 122 | + ( |
| 123 | + "x => x.HasMessages()", |
| 124 | + whereMapped.ToString() |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void Can_map_using_static_generic_method_call_to_test_the_parameter_expression() |
| 130 | + { |
| 131 | + //arrange |
| 132 | + var config = new MapperConfiguration(cfg => |
| 133 | + { |
| 134 | + cfg.AddExpressionMapping(); |
| 135 | + |
| 136 | + cfg.CreateMap<Shape, ShapeDto>() |
| 137 | + .ForMember(dto => dto.IsCircle, o => o.MapFrom(s => s.IsCircle<Shape>())); |
| 138 | + }); |
| 139 | + |
| 140 | + var mapper = config.CreateMapper(); |
| 141 | + Expression<Func<ShapeDto, bool>> where = x => x.IsCircle; |
| 142 | + |
| 143 | + //act |
| 144 | + Expression<Func<Shape, bool>> whereMapped = mapper.MapExpression<Expression<Func<Shape, bool>>>(where); |
| 145 | + var list = new List<Shape> { new Circle(), new Triangle()}.AsQueryable().Where(whereMapped).ToList(); |
| 146 | + |
| 147 | + //assert |
| 148 | + Assert.Single(list); |
| 149 | + Assert.Equal |
| 150 | + ( |
| 151 | + "x => x.IsCircle()", |
| 152 | + whereMapped.ToString() |
| 153 | + ); |
| 154 | + } |
| 155 | + |
| 156 | + public class ShapeHolder |
| 157 | + { |
| 158 | + public Shape Shape { get; set; } |
| 159 | + } |
| 160 | + |
| 161 | + public class Shape |
| 162 | + { |
| 163 | + public string Name { get; set; } |
| 164 | + public List<string> Messages { get; set; } |
| 165 | + } |
| 166 | + |
| 167 | + public class Triangle : Shape |
| 168 | + { |
| 169 | + public double A { get; set; } |
| 170 | + public double B { get; set; } |
| 171 | + public double C { get; set; } |
| 172 | + } |
| 173 | + |
| 174 | + public class Circle : Shape |
| 175 | + { |
| 176 | + public double R { get; set; } |
| 177 | + } |
| 178 | + |
| 179 | + public enum ShapeType |
| 180 | + { |
| 181 | + Unknown, |
| 182 | + Triangle, |
| 183 | + Circle, |
| 184 | + } |
| 185 | + |
| 186 | + public class ShapeDto |
| 187 | + { |
| 188 | + public string Name { get; set; } |
| 189 | + public IDictionary<string, object> DynamicProperties { get; set; } |
| 190 | + public ShapeType ShapeType { get; set; } |
| 191 | + public bool HasMany { get; set; } |
| 192 | + public bool IsCircle { get; set; } |
| 193 | + } |
| 194 | + |
| 195 | + public class ShapeHolderDto |
| 196 | + { |
| 197 | + public ShapeDto Shape { get; set; } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + public static class ShapeExtentions |
| 202 | + { |
| 203 | + public static bool HasMessages(this CanMapMemberFromTypeBinaryExpression.Shape shape) |
| 204 | + { |
| 205 | + return shape.Messages.Any(); |
| 206 | + } |
| 207 | + |
| 208 | + public static bool IsCircle<T>(this T shape) |
| 209 | + { |
| 210 | + return shape.GetType() == typeof(CanMapMemberFromTypeBinaryExpression.Circle); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments