-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathShouldThrowInvalidOperationExceptionForUnmatchedLiterals.cs
More file actions
156 lines (150 loc) · 6.69 KB
/
ShouldThrowInvalidOperationExceptionForUnmatchedLiterals.cs
File metadata and controls
156 lines (150 loc) · 6.69 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
using Microsoft.OData.Edm;
using System;
using System.Linq.Expressions;
using Xunit;
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class ShouldThrowInvalidOperationExceptionForUnmatchedLiterals
{
[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 ThrowsCreatingBinaryExpressionCombiningNonNullableParameterWithNullableConstant(string memberName, Type constantType)
{
ParameterExpression productParam = Expression.Parameter(typeof(ProductModel), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(ProductModel), memberName));
Assert.Throws<InvalidOperationException>
(
() => Expression.Lambda<Func<ProductModel, bool>>
(
Expression.Equal
(
property,
Expression.Constant(Activator.CreateInstance(constantType), constantType)
),
productParam
)
);
}
[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 ThrowsCreatingBinaryExpressionCombiningNullableParameterWithNonNullableConstant(string memberName, Type constantType)
{
ParameterExpression productParam = Expression.Parameter(typeof(Product), "x");
MemberExpression property = Expression.MakeMemberAccess(productParam, AutoMapper.Internal.TypeExtensions.GetFieldOrProperty(typeof(Product), memberName));
Assert.Throws<InvalidOperationException>
(
() => Expression.Lambda<Func<Product, bool>>
(
Expression.Equal
(
property,
Expression.Constant(Activator.CreateInstance(constantType), constantType)
),
productParam
)
);
}
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; }
}
}
}