-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathByteEnumTests.cs
More file actions
354 lines (308 loc) · 12.1 KB
/
Copy pathByteEnumTests.cs
File metadata and controls
354 lines (308 loc) · 12.1 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
using System;
using System.Linq;
using System.Linq.Expressions;
using NUnit.Framework;
namespace DelegateDecompiler.Tests
{
[TestFixture]
public class ByteEnumTests : DecompilerTestsBase
{
public enum TestEnum : byte
{
Foo,
Bar
}
[Flags]
public enum TestFlagEnum : byte
{
None = 0,
Foo = 1,
Bar = 2,
}
[Test]
public void TestEnumParameterEqualsEnumConstant()
{
Expression<Func<TestEnum, bool>> expected1 = x => x == TestEnum.Bar;
Expression<Func<TestEnum, bool>> expected2 = x => (int) x == (int) TestEnum.Bar;
Func<TestEnum, bool> compiled = x => x == TestEnum.Bar;
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumConstantEqualsEnumParameter()
{
Expression<Func<TestEnum, bool>> expected1 = x => TestEnum.Bar == x;
Expression<Func<TestEnum, bool>> expected2 = x => (int) TestEnum.Bar == (int) x;
Func<TestEnum, bool> compiled = x => TestEnum.Bar == x;
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumPropertyNotEqualsFooOrElseEnumPropertyEqualsBar()
{
Expression<Func<TestEnum, bool>> expected1 = x => (x != TestEnum.Bar) || (x == TestEnum.Foo);
Expression<Func<TestEnum, bool>> expected2 = x => ((int)x != (int)TestEnum.Bar) || ((int)x == (int)TestEnum.Foo);
Func<TestEnum, bool> compiled = x => (x != TestEnum.Bar) || (x == TestEnum.Foo);
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumPropertyEqualsFooOrElseEnumPropertyEqualsBar()
{
Expression<Func<TestEnum, bool>> expected1 = x => (x == TestEnum.Bar) || (x == TestEnum.Foo);
Expression<Func<TestEnum, bool>> expected2 = x => ((int)x == (int)TestEnum.Bar) || ((int)x == (int)TestEnum.Foo);
Func<TestEnum, bool> compiled = x => (x == TestEnum.Bar) || (x == TestEnum.Foo);
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumParametersEqual()
{
Expression<Func<TestEnum, TestEnum, bool>> expected = (x, y) => x == y;
Func<TestEnum, TestEnum, bool> compiled = (x, y) => x == y;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterNotEqualsEnumConstant()
{
Expression<Func<TestEnum, bool>> expected1 = x => x != TestEnum.Bar;
Expression<Func<TestEnum, bool>> expected2 = x => (int) x != (int) TestEnum.Bar;
Func<TestEnum, bool> compiled = x => x != TestEnum.Bar;
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumConstantNotEqualsEnumParameter()
{
Expression<Func<TestEnum, bool>> expected1 = x => TestEnum.Bar != x;
Expression<Func<TestEnum, bool>> expected2 = x => (int) TestEnum.Bar != (int) x;
Func<TestEnum, bool> compiled = x => TestEnum.Bar != x;
Test(compiled, expected1, expected2);
}
[Test]
public void TestEnumParametersNotEqual()
{
Expression<Func<TestEnum, TestEnum, bool>> expected = (x, y) => x != y;
Func<TestEnum, TestEnum, bool> compiled = (x, y) => x != y;
Test(compiled, expected);
}
[Test]
public void TestEnumConstantHasFlagEnumParameter()
{
Expression<Func<TestFlagEnum, bool>> expected = x => TestFlagEnum.Bar.HasFlag(x);
Func<TestFlagEnum, bool> compiled = x => TestFlagEnum.Bar.HasFlag(x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterHasFlagEnumConstant()
{
Expression<Func<TestFlagEnum, bool>> expected = x => x.HasFlag(TestFlagEnum.Bar);
Func<TestFlagEnum, bool> compiled = x => x.HasFlag(TestFlagEnum.Bar);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterHasFlagEnumParameter()
{
Expression<Func<TestFlagEnum, bool>> expected = x => x.HasFlag(x);
Func<TestFlagEnum, bool> compiled = x => x.HasFlag(x);
Test(compiled, expected);
}
[Test]
public void TestEnumConstantAndEnumParameter()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => TestFlagEnum.Bar & x;
Func<TestFlagEnum, TestFlagEnum> compiled = x => TestFlagEnum.Bar & x;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAndEnumConstant()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => x & TestFlagEnum.Bar ;
Func<TestFlagEnum, TestFlagEnum> compiled = x => x & TestFlagEnum.Bar ;
Test(compiled, expected);
}
[Test]
public void TestNotEnumParameter()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => ~ x;
Func<TestFlagEnum, TestFlagEnum> compiled = x => ~ x;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAndEnumParameter()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => x & x;
Func<TestFlagEnum, TestFlagEnum> compiled = x => x & x ;
Test(compiled, expected);
}
[Test]
public void TestEnumConstantOrEnumParameter()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => TestFlagEnum.Bar | x;
Func<TestFlagEnum, TestFlagEnum> compiled = x => TestFlagEnum.Bar | x;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterOrEnumConstant()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => x | TestFlagEnum.Bar ;
Func<TestFlagEnum, TestFlagEnum> compiled = x => x | TestFlagEnum.Bar ;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterOrEnumParameter()
{
Expression<Func<TestFlagEnum, TestFlagEnum>> expected = x => x | x;
Func<TestFlagEnum, TestFlagEnum> compiled = x => x | x ;
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodParameter()
{
Expression<Func<TestEnum, bool>> expected = x => TestEnumMethod(x);
Func<TestEnum, bool> compiled = x => TestEnumMethod(x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithEnumParameter()
{
Expression<Func<TestEnum, bool>> expected = x => EnumMethod(x);
Func<TestEnum, bool> compiled = x => EnumMethod(x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithObjectParameter()
{
Expression<Func<TestEnum, bool>> expected = x => ObjectMethod(x);
Func<TestEnum, bool> compiled = x => ObjectMethod(x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithInt8Parameter()
{
Expression<Func<TestEnum, bool>> expected = x => Int8Method((byte) x);
Func<TestEnum, bool> compiled = x => Int8Method((byte) x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithInt16Parameter()
{
Expression<Func<TestEnum, bool>> expected = x => Int16Method((short) x);
Func<TestEnum, bool> compiled = x => Int16Method((short) x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithInt32Parameter()
{
Expression<Func<TestEnum, bool>> expected = x => Int32Method((int) x);
Func<TestEnum, bool> compiled = x => Int32Method((int) x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsMethodWithInt64Parameter()
{
Expression<Func<TestEnum, bool>> expected = x => Int64Method((long) x);
Func<TestEnum, bool> compiled = x => Int64Method((long) x);
Test(compiled, expected);
}
[Test]
public void TestEnumParameterAsGenericMethodParameter()
{
Expression<Func<TestEnum, bool>> expected = x => GenericMethod(x);
Func<TestEnum, bool> compiled = x => GenericMethod(x);
Test(compiled, expected);
}
// The following tests check for the insertion of Expression.Convert in the expression tree for compatible types
[Test]
public void TestEnumCastSubtraction()
{
Expression<Func<TestEnum, int>> expected = x => (int)x - 10;
Func<TestEnum, int> compiled = x => (int)x - 10;
Test(compiled, expected);
}
[Test]
public void TestEnumCastMod()
{
Expression<Func<TestEnum, int>> expected = x => (int)x % 10;
Func<TestEnum, int> compiled = x => (int)x % 10;
Test(compiled, expected);
}
[Test]
public void TestEnumCastEquals()
{
Expression<Func<TestEnum, bool>> expected = x => (int)x == 10;
Func<TestEnum, bool> compiled = x => (int)x == 10;
Test(compiled, expected);
}
[Test]
public void TestEnumCastGreaterThan()
{
Expression<Func<TestEnum, bool>> expected = x => (int)x > 10;
Func<TestEnum, bool> compiled = x => (int)x > 10;
Test(compiled, expected);
}
[Test]
public void Issue61()
{
Expression<Func<decimal, decimal>> expected = x => Decimal.Round(x, 3, MidpointRounding.AwayFromZero);
Func<decimal, decimal> compiled = x => Decimal.Round(x, 3, MidpointRounding.AwayFromZero);
Test(compiled, expected);
}
[Test]
public void Issue98A()
{
Expression<Func<TestEnum?, TestEnum, bool>> expected = (x, y) => x == y;
Func<TestEnum?, TestEnum, bool> compiled = (x, y) => x == y;
Test(compiled, expected);
}
[Test]
public void Issue98B()
{
Expression<Func<TestEnum?, bool>> expected = x => x == TestEnum.Foo;
Func<TestEnum?, bool> compiled = x => x == TestEnum.Foo;
Test(compiled, expected);
}
[Test]
public void Issue160()
{
Expression<Func<int?, bool>> expected1 = x => (TestEnum?) x == TestEnum.Bar;
Expression<Func<int?, bool>> expected2 = x => (x.HasValue ? (TestEnum?) (x ?? 0) : null) == TestEnum.Bar;
Func<int?, bool> compiled = x => (TestEnum?) x == TestEnum.Bar;
Test(compiled, expected1, expected2);
}
[Test]
public void Issue176Array()
{
Expression<Func<TestEnum, bool>> expected = x => new [] {TestEnum.Foo, TestEnum.Bar}.Contains(x);
Func<TestEnum, bool> compiled = x => new[] {TestEnum.Foo, TestEnum.Bar}.Contains(x);
Test(compiled, expected);
}
private static bool TestEnumMethod(TestEnum p0)
{
throw new NotImplementedException();
}
private static bool EnumMethod(Enum p0)
{
throw new NotImplementedException();
}
private static bool ObjectMethod(object p0)
{
throw new NotImplementedException();
}
private static bool Int8Method(byte p0)
{
throw new NotImplementedException();
}
private static bool Int16Method(short p0)
{
throw new NotImplementedException();
}
private static bool Int32Method(int p0)
{
throw new NotImplementedException();
}
private static bool Int64Method(long p0)
{
throw new NotImplementedException();
}
private static bool GenericMethod<T>(T x)
{
throw new NotImplementedException();
}
}
}