forked from scottksmith95/LINQKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpressionCombinerTests.cs
235 lines (193 loc) · 9.49 KB
/
ExpressionCombinerTests.cs
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
using System;
using System.Linq;
using System.Linq.Expressions;
using Xunit;
#if !(NET35 || WINDOWS_APP || NETSTANDARD || PORTABLE40 || UAP)
using System.Runtime.CompilerServices;
#endif
namespace LinqKit.Tests.Net452
{
public class ExpressionCombinerTests
{
[Fact]
public void ExpressionCombiner_InvokeExpressionCombiner()
{
Expression<Func<Tuple<int, string>, bool>> criteria1 = x => x.Item1 > 1000;
Expression<Func<Tuple<int, string>, bool>> criteria2 = y => y.Item2.Contains("a");
Expression<Func<Tuple<int, string>, bool>> criteria3 = criteria1.Or(z => criteria2.Invoke(z));
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria3.Expand().ToString());
}
[Fact]
public void ExpressionCombiner_ExpressionOrElsePredicate()
{
Expression<Func<bool, bool, bool, bool>> criteria = (a, b, c) => a || b || c;
var exp = criteria.Expand().ToString();
Assert.Equal("(a, b, c) => ((a OrElse b) OrElse c)", exp);
}
[Fact]
public void ExpressionCombiner_ExpressionBalanceTest()
{
var exp = Expression.Equal(Expression.Constant(1), Expression.Constant(1));
var list = Enumerable.Repeat(exp, 50000).ToArray();
// Would throw stackoverflow with over 3100 items:
// var combined = list.Aggregate(Expression.OrElse);
// But this will work:
var combined = list.AggregateBalanced(Expression.OrElse);
var executed = combined.Expand().ToString();
Assert.Contains("(1 == 1)", executed);
}
public class Foo { public Bar bar; }
public class Bar { public bool baz; }
[Fact]
public void ExpressionCombiner_UnboundedVariableInExpandedPredicateTest()
{
Expression<Func<Foo, Bar>> barGetter = f => f.bar;
Expression<Func<Bar, bool>> barPredicate = b => b.baz;
Expression<Func<Foo, bool>> fooPredicate = x => barPredicate.Invoke(barGetter.Invoke(x));
Expression<Func<Foo, bool>> inception = y => fooPredicate.Invoke(y);
var expanded = inception.Expand(); // y => x.bar.baz
var compiled = expanded.Compile(); // throws an InvalidOperationException
var result = compiled.Invoke(new Foo { bar = new Bar() });
Assert.False(result);
}
[Fact]
public void ExpressionCombiner_ExpressionAsMethodVariable()
{
Expression<Func<Tuple<int, string>, bool>> criteria1 = x => x.Item1 > 1000;
Expression<Func<Tuple<int, string>, bool>> criteria2 = x => criteria1.Invoke(x) || x.Item2.Contains("a");
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria2.Expand().ToString());
}
Expression<Func<Tuple<int, string>, bool>> _ExpressionAsField_criteria1 = x => x.Item1 > 1000;
[Fact]
public void ExpressionCombiner_ExpressionAsField()
{
Expression<Func<Tuple<int, string>, bool>> criteria2 =
x => _ExpressionAsField_criteria1.Invoke(x) || x.Item2.Contains("a");
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria2.Expand().ToString());
}
Expression<Func<Tuple<int, string>, bool>> _ExpressionAsProperty_criteria1 => x => x.Item1 > 1000;
[Fact]
public void ExpressionCombiner_ExpressionAsProperty()
{
Expression<Func<Tuple<int, string>, bool>> criteria2 =
x => _ExpressionAsProperty_criteria1.Invoke(x) || x.Item2.Contains("a");
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria2.Expand().ToString());
}
private class _ExpressionAsProperty_FromExpressionVariable_criteria1
{
public Expression<Func<Tuple<int, string>, bool>> Expr => x => x.Item1 > 1000;
}
[Fact]
public void ExpressionCombiner_ExpressionAsProperty_FromExpressionVariable()
{
var criteria1obj = new _ExpressionAsProperty_FromExpressionVariable_criteria1();
Expression<Func<_ExpressionAsProperty_FromExpressionVariable_criteria1, Tuple<int, string>, bool>> getCriteria1 = (c, x) => c.Expr.Invoke(x);
Expression<Func<Tuple<int, string>, bool>> criteria2 =
x => getCriteria1.Invoke(criteria1obj, x) || x.Item2.Contains("a");
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria2.Expand().ToString());
}
private class _ExpressionAsField_FromExpressionVariable_criteria1
{
public Expression<Func<Tuple<int, string>, bool>> Expr = x => x.Item1 > 1000;
}
[Fact]
public void ExpressionCombiner_ExpressionAsField_FromExpressionVariable()
{
var criteria1obj = new _ExpressionAsField_FromExpressionVariable_criteria1();
Expression<Func<_ExpressionAsField_FromExpressionVariable_criteria1, Tuple<int, string>, bool>> getCriteria1 = (c, x) => c.Expr.Invoke(x);
Expression<Func<Tuple<int, string>, bool>> criteria2 =
x => getCriteria1.Invoke(criteria1obj, x) || x.Item2.Contains("a");
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
criteria2.Expand().ToString());
}
[Fact]
public void ExpressionCombiner_ExpressionAsParam()
{
Assert.Equal(
"x => ((x.Item1 > 1000) OrElse x.Item2.Contains(\"a\"))",
ExpressionCombiner_ExpressionAsParam_Method(x => x.Item1 > 1000));
}
private string ExpressionCombiner_ExpressionAsParam_Method(Expression<Func<Tuple<int, string>, bool>> criteria1)
{
Expression<Func<Tuple<int, string>, bool>> criteria2 =
x => criteria1.Invoke(x) || x.Item2.Contains("a");
return criteria2.Expand().ToString();
}
private int[] _possibleValues = new int[] { 1, 2, 3 };
[Fact]
public void ExpressionCombiner_ExpressionAsVariable_UsedAsParam()
{
Expression<Func<Tuple<int, string>, int>> valueExpr = x => x.Item1;
Expression<Func<Tuple<int, string>, bool>> criteria = x => _possibleValues.Contains(valueExpr.Invoke(x));
Assert.Equal(
"x => " + ConstExpressionString(() => _possibleValues) + ".Contains(x.Item1)",
criteria.Expand().ToString());
}
Expression<Func<Tuple<int, string>, int>> _ExpressionAsVariable_UsedAsParam_valueExpr = x => x.Item1;
[Fact]
public void ExpressionCombiner_ExpressionAsField_UsedAsParam()
{
Expression<Func<Tuple<int, string>, bool>> criteria = x => _possibleValues.Contains(_ExpressionAsVariable_UsedAsParam_valueExpr.Invoke(x));
Assert.Equal(
"x => " + ConstExpressionString(() => _possibleValues) + ".Contains(x.Item1)",
criteria.Expand().ToString());
}
Expression<Func<Tuple<int, string>, int>> _ExpressionAsProperty_UsedAsParam_valueExpr => x => x.Item1;
[Fact]
public void ExpressionCombiner_ExpressionAsProperty_UsedAsParam()
{
Expression<Func<Tuple<int, string>, bool>> criteria = x => _possibleValues.Contains(_ExpressionAsProperty_UsedAsParam_valueExpr.Invoke(x));
Assert.Equal(
"x => " + ConstExpressionString(() => _possibleValues) + ".Contains(x.Item1)",
criteria.Expand().ToString());
}
[Fact]
public void ExpressionCombiner_ExpressionAsParam_UsedAsParam()
{
Assert.Equal(
"x => " + ConstExpressionString(() => _possibleValues) + ".Contains(x.Item1)",
ExpressionAsParam_UsedAsParam_Method(x => x.Item1));
}
private string ExpressionAsParam_UsedAsParam_Method(Expression<Func<Tuple<int, string>, int>> valueExpr)
{
Expression<Func<Tuple<int, string>, bool>> criteria = x => _possibleValues.Contains(valueExpr.Invoke(x));
return criteria.Expand().ToString();
}
[Fact]
public void NestingMemberAccess()
{
Expression<Func<Tuple<Tuple<int, DateTime>, string>, Tuple<int, DateTime>>> memberExpr1 = x => x.Item1;
Expression<Func<Tuple<int, DateTime>, DateTime>> memberExpr2 = x => x.Item2;
Expression<Func<Tuple<Tuple<int, DateTime>, string>, DateTime>> criteria =
x => memberExpr2.Invoke(memberExpr1.Invoke(x));
Assert.Equal(
"x => x.Item1.Item2",
criteria.Expand().ToString());
}
[Fact]
public void ExpandProcessesArguments()
{
Expression<Func<Tuple<bool, bool>, bool>> expr1 = x => x.Item1 && x.Item2;
Expression<Func<bool, Tuple<bool, bool>>> expr2 = y => new Tuple<bool, bool>(y, y);
Expression<Func<bool, bool>> nestedExpression = z => expr1.Invoke(expr2.Invoke(z));
Assert.Equal(
nestedExpression.Expand().ToString(),
nestedExpression.Expand().Expand().ToString());
}
private string ConstExpressionString<TResult>(Expression<Func<TResult>> expr)
{
return expr.ToString().Substring(6);
}
}
}