-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathCanMapMismatchedLiteralMemberExpressionsWithoutCustomExpressions.cs
More file actions
293 lines (276 loc) · 13.5 KB
/
CanMapMismatchedLiteralMemberExpressionsWithoutCustomExpressions.cs
File metadata and controls
293 lines (276 loc) · 13.5 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
290
291
292
293
using Microsoft.OData.Edm;
using System;
using System.Linq.Expressions;
using Xunit;
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapMismatchedLiteralMemberExpressionsWithoutCustomExpressions
{
[Theory]
[InlineData(nameof(ProductModel.Bool), typeof(bool))]
[InlineData(nameof(ProductModel.DateTime), typeof(DateTime))]
[InlineData(nameof(ProductModel.DateTimeOffset), typeof(DateTimeOffset))]
[InlineData(nameof(ProductModel.Date), typeof(Date))]
#if NET8_0_OR_GREATER
[InlineData(nameof(ProductModel.DateOnly), typeof(DateOnly))]
#endif
[InlineData(nameof(ProductModel.TimeSpan), typeof(TimeSpan))]
[InlineData(nameof(ProductModel.TimeOfDay), typeof(TimeOfDay))]
#if NET8_0_OR_GREATER
[InlineData(nameof(ProductModel.TimeOnly), typeof(TimeOnly))]
#endif
[InlineData(nameof(ProductModel.Guid), typeof(Guid))]
[InlineData(nameof(ProductModel.Decimal), typeof(decimal))]
[InlineData(nameof(ProductModel.Byte), typeof(byte))]
[InlineData(nameof(ProductModel.Short), typeof(short))]
[InlineData(nameof(ProductModel.Int), typeof(int))]
[InlineData(nameof(ProductModel.Long), typeof(long))]
[InlineData(nameof(ProductModel.Float), typeof(float))]
[InlineData(nameof(ProductModel.Double), typeof(double))]
[InlineData(nameof(ProductModel.Char), typeof(char))]
[InlineData(nameof(ProductModel.SByte), typeof(sbyte))]
[InlineData(nameof(ProductModel.UShort), typeof(ushort))]
[InlineData(nameof(ProductModel.ULong), typeof(ulong))]
public void CanMapNonNullableToNullableWithoutCustomExpression(string memberName, Type constantType)
{
//arrange
var mapper = GetDataToModelMapper();
ParameterExpression productParam = Expression.Parameter(typeof(ProductModel), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(ProductModel), memberName));
object constantValue = Activator.CreateInstance(constantType);
Expression<Func<ProductModel, bool>> expression = Expression.Lambda<Func<ProductModel, bool>>
(
Expression.Equal
(
property,
Expression.Constant(constantValue, constantType)
),
productParam
);
Product product = new();
typeof(Product).GetProperty(memberName).SetValue(product, constantValue);
//act
var mappedExpression = mapper.MapExpression<Expression<Func<Product, bool>>>(expression);
//assert
Assert.True(mappedExpression.Compile()(product));
}
[Theory]
[InlineData(nameof(Product.Bool), typeof(bool?))]
[InlineData(nameof(Product.DateTime), typeof(DateTime?))]
[InlineData(nameof(Product.DateTimeOffset), typeof(DateTimeOffset?))]
[InlineData(nameof(Product.Date), typeof(Date?))]
#if NET8_0_OR_GREATER
[InlineData(nameof(Product.DateOnly), typeof(DateOnly?))]
#endif
[InlineData(nameof(Product.TimeSpan), typeof(TimeSpan?))]
[InlineData(nameof(Product.TimeOfDay), typeof(TimeOfDay?))]
#if NET8_0_OR_GREATER
[InlineData(nameof(Product.TimeOnly), typeof(TimeOnly?))]
#endif
[InlineData(nameof(Product.Guid), typeof(Guid?))]
[InlineData(nameof(Product.Decimal), typeof(decimal?))]
[InlineData(nameof(Product.Byte), typeof(byte?))]
[InlineData(nameof(Product.Short), typeof(short?))]
[InlineData(nameof(Product.Int), typeof(int?))]
[InlineData(nameof(Product.Long), typeof(long?))]
[InlineData(nameof(Product.Float), typeof(float?))]
[InlineData(nameof(Product.Double), typeof(double?))]
[InlineData(nameof(Product.Char), typeof(char?))]
[InlineData(nameof(Product.SByte), typeof(sbyte?))]
[InlineData(nameof(Product.UShort), typeof(ushort?))]
[InlineData(nameof(Product.ULong), typeof(ulong?))]
public void CanMapNullableToNonNullableWithoutCustomExpression(string memberName, Type constantType)
{
//arrange
var mapper = GetModelToDataMapper();
ParameterExpression productParam = Expression.Parameter(typeof(Product), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(Product), memberName));
object constantValue = Activator.CreateInstance(Nullable.GetUnderlyingType(constantType));
Expression<Func<Product, bool>> expression = Expression.Lambda<Func<Product, bool>>
(
Expression.Equal
(
property,
Expression.Constant(constantValue, constantType)
),
productParam
);
ProductModel product = new();
typeof(ProductModel).GetProperty(memberName).SetValue(product, constantValue);
//act
var mappedExpression = mapper.MapExpression<Expression<Func<ProductModel, bool>>>(expression);
//assert
Assert.True(mappedExpression.Compile()(product));
}
[Theory]
[InlineData(nameof(ProductModel.Bool), typeof(bool))]
[InlineData(nameof(ProductModel.DateTime), typeof(DateTime))]
[InlineData(nameof(ProductModel.DateTimeOffset), typeof(DateTimeOffset))]
[InlineData(nameof(ProductModel.Date), typeof(Date))]
#if NET8_0_OR_GREATER
[InlineData(nameof(ProductModel.DateOnly), typeof(DateOnly))]
#endif
[InlineData(nameof(ProductModel.TimeSpan), typeof(TimeSpan))]
[InlineData(nameof(ProductModel.TimeOfDay), typeof(TimeOfDay))]
#if NET8_0_OR_GREATER
[InlineData(nameof(ProductModel.TimeOnly), typeof(TimeOnly))]
#endif
[InlineData(nameof(ProductModel.Guid), typeof(Guid))]
[InlineData(nameof(ProductModel.Decimal), typeof(decimal))]
[InlineData(nameof(ProductModel.Byte), typeof(byte))]
[InlineData(nameof(ProductModel.Short), typeof(short))]
[InlineData(nameof(ProductModel.Int), typeof(int))]
[InlineData(nameof(ProductModel.Long), typeof(long))]
[InlineData(nameof(ProductModel.Float), typeof(float))]
[InlineData(nameof(ProductModel.Double), typeof(double))]
[InlineData(nameof(ProductModel.Char), typeof(char))]
[InlineData(nameof(ProductModel.SByte), typeof(sbyte))]
[InlineData(nameof(ProductModel.UShort), typeof(ushort))]
[InlineData(nameof(ProductModel.ULong), typeof(ulong))]
public void CanMapNonNullableSelectorToNullableelectorWithoutCustomExpression(string memberName, Type memberType)
{
var mapper = GetDataToModelMapper();
ParameterExpression productParam = Expression.Parameter(typeof(ProductModel), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(ProductModel), memberName));
Type sourceType = typeof(Func<,>).MakeGenericType(typeof(ProductModel), memberType);
Type destType = typeof(Func<,>).MakeGenericType(typeof(Product), memberType);
Type sourceExpressionype = typeof(Expression<>).MakeGenericType(sourceType);
Type destExpressionType = typeof(Expression<>).MakeGenericType(destType);
var expression = Expression.Lambda
(
sourceType,
property,
productParam
);
object constantValue = Activator.CreateInstance(memberType);
Product product = new();
typeof(Product).GetProperty(memberName).SetValue(product, constantValue);
//act
var mappedExpression = mapper.MapExpression(expression, sourceExpressionype, destExpressionType);
//assert
Assert.Equal(constantValue, mappedExpression.Compile().DynamicInvoke(product));
}
[Theory]
[InlineData(nameof(Product.Bool), typeof(bool?))]
[InlineData(nameof(Product.DateTime), typeof(DateTime?))]
[InlineData(nameof(Product.DateTimeOffset), typeof(DateTimeOffset?))]
[InlineData(nameof(Product.Date), typeof(Date?))]
#if NET8_0_OR_GREATER
[InlineData(nameof(Product.DateOnly), typeof(DateOnly?))]
#endif
[InlineData(nameof(Product.TimeSpan), typeof(TimeSpan?))]
[InlineData(nameof(Product.TimeOfDay), typeof(TimeOfDay?))]
#if NET8_0_OR_GREATER
[InlineData(nameof(Product.TimeOnly), typeof(TimeOnly?))]
#endif
[InlineData(nameof(Product.Guid), typeof(Guid?))]
[InlineData(nameof(Product.Decimal), typeof(decimal?))]
[InlineData(nameof(Product.Byte), typeof(byte?))]
[InlineData(nameof(Product.Short), typeof(short?))]
[InlineData(nameof(Product.Int), typeof(int?))]
[InlineData(nameof(Product.Long), typeof(long?))]
[InlineData(nameof(Product.Float), typeof(float?))]
[InlineData(nameof(Product.Double), typeof(double?))]
[InlineData(nameof(Product.Char), typeof(char?))]
[InlineData(nameof(Product.SByte), typeof(sbyte?))]
[InlineData(nameof(Product.UShort), typeof(ushort?))]
[InlineData(nameof(Product.ULong), typeof(ulong?))]
public void CanMapNullableSelectorToNonNullableelectorWithoutCustomExpression(string memberName, Type memberType)
{
var mapper = GetModelToDataMapper();
ParameterExpression productParam = Expression.Parameter(typeof(Product), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(Product), memberName));
Type sourceType = typeof(Func<,>).MakeGenericType(typeof(Product), memberType);
Type destType = typeof(Func<,>).MakeGenericType(typeof(ProductModel), memberType);
Type sourceExpressionype = typeof(Expression<>).MakeGenericType(sourceType);
Type destExpressionType = typeof(Expression<>).MakeGenericType(destType);
var expression = Expression.Lambda
(
sourceType,
property,
productParam
);
object constantValue = Activator.CreateInstance(Nullable.GetUnderlyingType(memberType));
ProductModel product = new();
typeof(ProductModel).GetProperty(memberName).SetValue(product, constantValue);
//act
var mappedExpression = mapper.MapExpression(expression, sourceExpressionype, destExpressionType);
//assert
Assert.Equal(constantValue, mappedExpression.Compile().DynamicInvoke(product));
}
private static IMapper GetModelToDataMapper()
{
var config = ConfigurationHelper.GetMapperConfiguration(c =>
{
c.CreateMap<ProductModel, Product>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
private static IMapper GetDataToModelMapper()
{
var config = ConfigurationHelper.GetMapperConfiguration(c =>
{
c.CreateMap<Product, ProductModel>();
});
config.AssertConfigurationIsValid();
return config.CreateMapper();
}
class Product
{
public bool? Bool { get; set; }
public DateTimeOffset? DateTimeOffset { get; set; }
public DateTime? DateTime { get; set; }
public Date? Date { get; set; }
#if NET8_0_OR_GREATER
public DateOnly? DateOnly { get; set; }
#endif
public TimeSpan? TimeSpan { get; set; }
public TimeOfDay? TimeOfDay { get; set; }
#if NET8_0_OR_GREATER
public TimeOnly? TimeOnly { get; set; }
#endif
public Guid? Guid { get; set; }
public decimal? Decimal { get; set; }
public byte? Byte { get; set; }
public short? Short { get; set; }
public int? Int { get; set; }
public long? Long { get; set; }
public float? Float { get; set; }
public double? Double { get; set; }
public char? Char { get; set; }
public sbyte? SByte { get; set; }
public ushort? UShort { get; set; }
public uint? UInt { get; set; }
public ulong? ULong { get; set; }
}
class ProductModel
{
public bool Bool { get; set; }
public DateTimeOffset DateTimeOffset { get; set; }
public DateTime DateTime { get; set; }
public Date Date { get; set; }
#if NET8_0_OR_GREATER
public DateOnly DateOnly { get; set; }
#endif
public TimeSpan TimeSpan { get; set; }
public TimeOfDay TimeOfDay { get; set; }
#if NET8_0_OR_GREATER
public TimeOnly TimeOnly { get; set; }
#endif
public Guid Guid { get; set; }
public decimal Decimal { get; set; }
public byte Byte { get; set; }
public short Short { get; set; }
public int Int { get; set; }
public long Long { get; set; }
public float Float { get; set; }
public double Double { get; set; }
public char Char { get; set; }
public sbyte SByte { get; set; }
public ushort UShort { get; set; }
public uint UInt { get; set; }
public ulong ULong { get; set; }
}
}
}