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
+
1
5
using System ;
2
6
using System . Linq . Expressions ;
3
- using System . Collections . Generic ;
4
- using System . Text ;
7
+ using NUnit . Framework ;
5
8
using Xtensive . Reflection ;
6
9
using Xtensive . Orm . Linq . Expressions . Visitors ;
7
- using NUnit . Framework ;
8
- using System . Linq ;
10
+
9
11
10
12
namespace Xtensive . Orm . Tests . Core . Linq
11
13
{
12
- public class EnumRewriterTest
14
+ public class EnumRewriterTest
13
15
{
14
16
private enum ByteBasedEnum : byte
15
17
{
@@ -52,39 +54,59 @@ private enum ULongBasedEnum : ulong
52
54
}
53
55
54
56
55
- private Expression [ ] Expressions ;
57
+ private Expression [ ] ConstExpressions ;
58
+ private Expression [ ] NonNullableExpressions ;
59
+ private Expression [ ] NullableExpressions ;
60
+ private Expression [ ] NullExpressions ;
56
61
57
62
[ OneTimeSetUp ]
58
63
public void TestFixtureSetUp ( )
59
64
{
60
- Expressions = new [ ] {
65
+ ConstExpressions = new [ ] {
61
66
// non-enum constants
62
67
Expression . Constant ( 1 , typeof ( int ) ) ,
63
68
Expression . Constant ( 2 , typeof ( int ? ) ) ,
64
69
Expression . Constant ( null , typeof ( int ? ) ) ,
70
+ } ;
65
71
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 ) ) ,
68
75
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 ) ) ,
72
79
Expression . Constant ( LongBasedEnum . Value1 , typeof ( LongBasedEnum ) ) ,
80
+ Expression . Constant ( ULongBasedEnum . Value1 , typeof ( ULongBasedEnum ) ) ,
81
+ } ;
73
82
83
+ NullableExpressions = new [ ] {
84
+ Expression . Constant ( ByteBasedEnum . Value2 , typeof ( ByteBasedEnum ? ) ) ,
85
+ Expression . Constant ( SByteBasedEnum . Value2 , typeof ( SByteBasedEnum ? ) ) ,
74
86
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 ? ) ) ,
76
90
Expression . Constant ( LongBasedEnum . Value2 , typeof ( LongBasedEnum ? ) ) ,
91
+ Expression . Constant ( ULongBasedEnum . Value2 , typeof ( ULongBasedEnum ? ) ) ,
92
+ } ;
77
93
94
+ NullExpressions = new [ ] {
95
+ Expression . Constant ( null , typeof ( ByteBasedEnum ? ) ) ,
96
+ Expression . Constant ( null , typeof ( SByteBasedEnum ? ) ) ,
78
97
Expression . Constant ( null , typeof ( ShortBasedEnum ? ) ) ,
98
+ Expression . Constant ( null , typeof ( UShortBasedEnum ? ) ) ,
79
99
Expression . Constant ( null , typeof ( IntBasedEnum ? ) ) ,
100
+ Expression . Constant ( null , typeof ( UIntBasedEnum ? ) ) ,
80
101
Expression . Constant ( null , typeof ( LongBasedEnum ? ) ) ,
102
+ Expression . Constant ( null , typeof ( ULongBasedEnum ? ) ) ,
81
103
} ;
82
104
}
83
105
84
106
[ Test ]
85
107
public void NonEnumValuesTest ( )
86
108
{
87
- foreach ( var exp in Expressions . Take ( 3 ) ) {
109
+ foreach ( var exp in ConstExpressions ) {
88
110
var rewrited = EnumRewriter . Rewrite ( exp ) ;
89
111
Assert . That ( rewrited , Is . EqualTo ( exp ) ) ;
90
112
}
@@ -93,7 +115,7 @@ public void NonEnumValuesTest()
93
115
[ Test ]
94
116
public void NonNullableEnumsTest ( )
95
117
{
96
- foreach ( var exp in Expressions . Skip ( 3 ) . Take ( 3 ) ) {
118
+ foreach ( var exp in NonNullableExpressions ) {
97
119
var rewrited = EnumRewriter . Rewrite ( exp ) ;
98
120
var expType = exp . Type ;
99
121
var enumType = expType . StripNullable ( ) ;
@@ -112,7 +134,7 @@ public void NonNullableEnumsTest()
112
134
[ Test ]
113
135
public void NullableEnumsTest ( )
114
136
{
115
- foreach ( var exp in Expressions . Skip ( 6 ) . Take ( 3 ) ) {
137
+ foreach ( var exp in NullableExpressions ) {
116
138
var rewrited = EnumRewriter . Rewrite ( exp ) ;
117
139
var expType = exp . Type ;
118
140
var enumType = expType . StripNullable ( ) ;
@@ -134,7 +156,7 @@ public void NullableEnumsTest()
134
156
[ Test ]
135
157
public void NullsAsNullableEnumsTest ( )
136
158
{
137
- foreach ( var exp in Expressions . Skip ( 9 ) . Take ( 3 ) ) {
159
+ foreach ( var exp in NullExpressions ) {
138
160
139
161
var rewrited = EnumRewriter . Rewrite ( exp ) ;
140
162
var expType = exp . Type ;
@@ -152,35 +174,5 @@ public void NullsAsNullableEnumsTest()
152
174
Assert . That ( constant . Value , Is . Null ) ;
153
175
}
154
176
}
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
- //}
185
177
}
186
178
}
0 commit comments