Skip to content

Commit c695cbe

Browse files
committed
Improve EnumRewriterTest
1 parent d055153 commit c695cbe

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

Orm/Xtensive.Orm.Tests.Core/Linq/EnumRewriterTest.cs

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
// Copyright (C) 2024 Xtensive LLC.
2+
// This code is distributed under MIT license terms.
3+
// See the License.txt file in the project root for more information.
4+
15
using System;
26
using System.Linq.Expressions;
3-
using System.Collections.Generic;
4-
using System.Text;
7+
using NUnit.Framework;
58
using Xtensive.Reflection;
69
using Xtensive.Orm.Linq.Expressions.Visitors;
7-
using NUnit.Framework;
8-
using System.Linq;
10+
911

1012
namespace Xtensive.Orm.Tests.Core.Linq
1113
{
12-
public class EnumRewriterTest
14+
public class EnumRewriterTest
1315
{
1416
private enum ByteBasedEnum : byte
1517
{
@@ -52,39 +54,59 @@ private enum ULongBasedEnum : ulong
5254
}
5355

5456

55-
private Expression[] Expressions;
57+
private Expression[] ConstExpressions;
58+
private Expression[] NonNullableExpressions;
59+
private Expression[] NullableExpressions;
60+
private Expression[] NullExpressions;
5661

5762
[OneTimeSetUp]
5863
public void TestFixtureSetUp()
5964
{
60-
Expressions = new[] {
65+
ConstExpressions = new[] {
6166
// non-enum constants
6267
Expression.Constant(1, typeof(int)),
6368
Expression.Constant(2, typeof(int?)),
6469
Expression.Constant(null, typeof(int?)),
70+
};
6571

66-
//short enums
67-
Expression.Constant(ShortBasedEnum.Value1, typeof(ShortBasedEnum)),
72+
NonNullableExpressions = new[] {
73+
Expression.Constant(ByteBasedEnum.Value1, typeof(ByteBasedEnum)),
74+
Expression.Constant(SByteBasedEnum.Value1, typeof(SByteBasedEnum)),
6875
Expression.Constant(ShortBasedEnum.Value1, typeof(ShortBasedEnum)),
69-
Expression.Constant(ShortBasedEnum.Value1, typeof(ShortBasedEnum)),
70-
Expression.Constant(ShortBasedEnum.Value1, typeof(ShortBasedEnum)),
71-
Expression.Constant(IntBasedEnum.Value1, typeof(IntBasedEnum)),
76+
Expression.Constant(UShortBasedEnum.Value1, typeof(UShortBasedEnum)),
77+
Expression.Constant(IntBasedEnum.Value1, typeof(IntBasedEnum)),
78+
Expression.Constant(UIntBasedEnum.Value1, typeof(UIntBasedEnum)),
7279
Expression.Constant(LongBasedEnum.Value1, typeof(LongBasedEnum)),
80+
Expression.Constant(ULongBasedEnum.Value1, typeof(ULongBasedEnum)),
81+
};
7382

83+
NullableExpressions = new[] {
84+
Expression.Constant(ByteBasedEnum.Value2, typeof(ByteBasedEnum?)),
85+
Expression.Constant(SByteBasedEnum.Value2, typeof(SByteBasedEnum?)),
7486
Expression.Constant(ShortBasedEnum.Value2, typeof(ShortBasedEnum?)),
75-
Expression.Constant(IntBasedEnum.Value2, typeof(IntBasedEnum?)),
87+
Expression.Constant(UShortBasedEnum.Value2, typeof(UShortBasedEnum?)),
88+
Expression.Constant(IntBasedEnum.Value2, typeof(IntBasedEnum?)),
89+
Expression.Constant(UIntBasedEnum.Value2, typeof(UIntBasedEnum?)),
7690
Expression.Constant(LongBasedEnum.Value2, typeof(LongBasedEnum?)),
91+
Expression.Constant(ULongBasedEnum.Value2, typeof(ULongBasedEnum?)),
92+
};
7793

94+
NullExpressions = new[] {
95+
Expression.Constant(null, typeof(ByteBasedEnum?)),
96+
Expression.Constant(null, typeof(SByteBasedEnum?)),
7897
Expression.Constant(null, typeof(ShortBasedEnum?)),
98+
Expression.Constant(null, typeof(UShortBasedEnum?)),
7999
Expression.Constant(null, typeof(IntBasedEnum?)),
100+
Expression.Constant(null, typeof(UIntBasedEnum?)),
80101
Expression.Constant(null, typeof(LongBasedEnum?)),
102+
Expression.Constant(null, typeof(ULongBasedEnum?)),
81103
};
82104
}
83105

84106
[Test]
85107
public void NonEnumValuesTest()
86108
{
87-
foreach (var exp in Expressions.Take(3)) {
109+
foreach (var exp in ConstExpressions) {
88110
var rewrited = EnumRewriter.Rewrite(exp);
89111
Assert.That(rewrited, Is.EqualTo(exp));
90112
}
@@ -93,7 +115,7 @@ public void NonEnumValuesTest()
93115
[Test]
94116
public void NonNullableEnumsTest()
95117
{
96-
foreach (var exp in Expressions.Skip(3).Take(3)) {
118+
foreach (var exp in NonNullableExpressions) {
97119
var rewrited = EnumRewriter.Rewrite(exp);
98120
var expType = exp.Type;
99121
var enumType = expType.StripNullable();
@@ -112,7 +134,7 @@ public void NonNullableEnumsTest()
112134
[Test]
113135
public void NullableEnumsTest()
114136
{
115-
foreach (var exp in Expressions.Skip(6).Take(3)) {
137+
foreach (var exp in NullableExpressions) {
116138
var rewrited = EnumRewriter.Rewrite(exp);
117139
var expType = exp.Type;
118140
var enumType = expType.StripNullable();
@@ -134,7 +156,7 @@ public void NullableEnumsTest()
134156
[Test]
135157
public void NullsAsNullableEnumsTest()
136158
{
137-
foreach (var exp in Expressions.Skip(9).Take(3)) {
159+
foreach (var exp in NullExpressions) {
138160

139161
var rewrited = EnumRewriter.Rewrite(exp);
140162
var expType = exp.Type;
@@ -152,35 +174,5 @@ public void NullsAsNullableEnumsTest()
152174
Assert.That(constant.Value, Is.Null);
153175
}
154176
}
155-
156-
//[Test]
157-
//public void ComplexTest()
158-
//{
159-
// foreach (var exp in Expressions) {
160-
// var rewrited = EnumRewriter.Rewrite(exp);
161-
// var expType = exp.Type;
162-
// if (exp is ConstantExpression testExp && expType.StripNullable().IsEnum) {
163-
// var isNullable = expType.IsNullable();
164-
// var enumType = expType.StripNullable();
165-
// if (isNullable) {
166-
167-
// }
168-
// else {
169-
// Assert.That(rewrited, Is.InstanceOf<UnaryExpression>());
170-
// var convert = rewrited as UnaryExpression;
171-
// Assert.That(convert.NodeType, Is.EqualTo(ExpressionType.Convert));
172-
// Assert.That(convert.Type, Is.EqualTo(expType));
173-
// var operand = convert.Operand;
174-
// Assert.That(operand, Is.InstanceOf<ConstantExpression>());
175-
// var constant = operand as ConstantExpression;
176-
// Assert.That(constant.Type, Is.Not.EqualTo(enumType));
177-
// Assert.That(constant.Type, Is.EqualTo(Enum.GetUnderlyingType(enumType)));
178-
// }
179-
// }
180-
// else {
181-
// Assert.That(rewrited, Is.EqualTo(exp));
182-
// }
183-
// }
184-
//}
185177
}
186178
}

0 commit comments

Comments
 (0)