forked from zzzprojects/System.Linq.Dynamic.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicExpressionParserTests.cs
More file actions
352 lines (294 loc) · 12.9 KB
/
DynamicExpressionParserTests.cs
File metadata and controls
352 lines (294 loc) · 12.9 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
using NFluent;
using System.Collections.Generic;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Expressions;
using Xunit;
using User = System.Linq.Dynamic.Core.Tests.Helpers.Models.User;
namespace System.Linq.Dynamic.Core.Tests
{
public class DynamicExpressionParserTests
{
private class MyClass
{
public int Foo()
{
return 42;
}
}
private class ComplexParseLambda1Result
{
public int? Age;
public int TotalIncome;
}
[Fact]
public void ParseLambda_ToList()
{
// Arrange
var testList = User.GenerateSampleModels(51);
var qry = testList.AsQueryable();
// Act
string query = "OrderBy(gg => gg.Income).ToList()";
LambdaExpression expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, query);
Delegate del = expression.Compile();
IEnumerable<dynamic> result = del.DynamicInvoke(qry) as IEnumerable<dynamic>;
var expected = qry.OrderBy(gg => gg.Income).ToList();
// Assert
Check.That(result).IsNotNull();
Check.That(result).HasSize(expected.Count);
Check.That(result.ToArray()[0]).Equals(expected[0]);
}
[Fact]
public void ParseLambda_Complex_1()
{
// Arrange
var testList = User.GenerateSampleModels(51);
var qry = testList.AsQueryable();
var externals = new Dictionary<string, object>
{
{ "Users", qry }
};
// Act
string query = "Users.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))";
LambdaExpression expression = DynamicExpressionParser.ParseLambda(null, query, externals);
Delegate del = expression.Compile();
IEnumerable<dynamic> result = del.DynamicInvoke() as IEnumerable<dynamic>;
var expected = qry.GroupBy(x => new { x.Profile.Age }).OrderBy(gg => gg.Key.Age).Select(j => new { j.Key.Age, TotalIncome = j.Sum(k => k.Income) }).Select(c => new ComplexParseLambda1Result { Age = c.Age, TotalIncome = c.TotalIncome }).Cast<dynamic>().ToArray();
// Assert
Check.That(result).IsNotNull();
Check.That(result).HasSize(expected.Length);
Check.That(result.ToArray()[0]).Equals(expected[0]);
}
[Fact]
public void ParseLambda_Complex_2()
{
// Arrange
var testList = User.GenerateSampleModels(51);
var qry = testList.AsQueryable();
// Act
string query = "GroupBy(x => new { x.Profile.Age }, it).OrderBy(gg => gg.Key.Age).Select(j => new (j.Key.Age, j.Sum(k => k.Income) As TotalIncome))";
LambdaExpression expression = DynamicExpressionParser.ParseLambda(qry.GetType(), null, query);
Delegate del = expression.Compile();
IEnumerable<dynamic> result = del.DynamicInvoke(qry) as IEnumerable<dynamic>;
var expected = qry.GroupBy(x => new { x.Profile.Age }, x => x).OrderBy(gg => gg.Key.Age).Select(j => new { j.Key.Age, TotalIncome = j.Sum(k => k.Income) }).Select(c => new ComplexParseLambda1Result { Age = c.Age, TotalIncome = c.TotalIncome }).Cast<dynamic>().ToArray();
// Assert
Check.That(result).IsNotNull();
Check.That(result).HasSize(expected.Length);
Check.That(result.ToArray()[0]).Equals(expected[0]);
}
[Fact]
public void ParseLambda_Select_1()
{
// Arrange
var testList = User.GenerateSampleModels(51);
var qry = testList.AsQueryable();
var externals = new Dictionary<string, object>
{
{"Users", qry}
};
// Act
string query = "Users.Select(j => new User(j.Income As Income))";
LambdaExpression expression = DynamicExpressionParser.ParseLambda(null, query, externals);
Delegate del = expression.Compile();
object result = del.DynamicInvoke();
// Assert
Assert.NotNull(result);
}
[Fact]
public void ParseLambda_Select_2()
{
// Arrange
var testList = User.GenerateSampleModels(5);
var qry = testList.AsQueryable();
var externals = new Dictionary<string, object>
{
{"Users", qry}
};
// Act
string query = "Users.Select(j => j)";
LambdaExpression expression = DynamicExpressionParser.ParseLambda(null, query, externals);
Delegate del = expression.Compile();
object result = del.DynamicInvoke();
// Assert
Assert.NotNull(result);
}
// https://github.com/StefH/System.Linq.Dynamic.Core/issues/58
[Fact]
public void ParseLambda_4_Issue58()
{
var expressionParams = new[]
{
Expression.Parameter(typeof (MyClass), "myObj")
};
var myClassInstance = new MyClass();
var invokersMerge = new List<object> { myClassInstance };
LambdaExpression expression = DynamicExpressionParser.ParseLambda(false, expressionParams, null, "myObj.Foo()");
Delegate del = expression.Compile();
object result = del.DynamicInvoke(invokersMerge.ToArray());
Check.That(result).Equals(42);
}
[Fact]
public void ParseLambda_DuplicateParameterNames_ThrowsException()
{
// Arrange
var parameters = new[]
{
Expression.Parameter(typeof(int), "x"),
Expression.Parameter(typeof(int), "x")
};
// Act and Assert
Check.ThatCode(() => DynamicExpressionParser.ParseLambda(parameters, typeof(bool), "it == 42"))
.Throws<ParseException>()
.WithMessage("The identifier 'x' was defined more than once");
}
[Fact]
public void ParseLambda_EmptyParameterList()
{
// Arrange
var pEmpty = new ParameterExpression[] { };
// Act
var @delegate = DynamicExpressionParser.ParseLambda(pEmpty, null, "1+2").Compile();
int? result = @delegate.DynamicInvoke() as int?;
// Assert
Check.That(result).Equals(3);
}
[Fact]
public void ParseLambda_ParameterName()
{
// Arrange
var parameters = new[]
{
Expression.Parameter(typeof(int), "x")
};
// Assert
var expressionX = DynamicExpressionParser.ParseLambda(parameters, typeof(bool), "x == 42");
var expressionIT = DynamicExpressionParser.ParseLambda(parameters, typeof(bool), "it == 42");
// Assert
Assert.Equal(typeof(bool), expressionX.Body.Type);
Assert.Equal(typeof(bool), expressionIT.Body.Type);
}
[Fact]
public void ParseLambda_ParameterName_Empty()
{
// Arrange
var parameters = new[]
{
Expression.Parameter(typeof(int), "")
};
// Assert
var expression = DynamicExpressionParser.ParseLambda(parameters, typeof(bool), "it == 42");
// Assert
Assert.Equal(typeof(bool), expression.Body.Type);
}
[Fact]
public void ParseLambda_ParameterName_Null()
{
// Arrange
var parameters = new[]
{
Expression.Parameter(typeof(int), null)
};
// Assert
var expression = DynamicExpressionParser.ParseLambda(parameters, typeof(bool), "it == 42");
// Assert
Assert.Equal(typeof(bool), expression.Body.Type);
}
[Fact]
public void ParseLambda_ParameterExpressionMethodCall_ReturnsIntExpression()
{
var expression = DynamicExpressionParser.ParseLambda(true,
new[] { Expression.Parameter(typeof(int), "x") },
typeof(int),
"x + 1");
Assert.Equal(typeof(int), expression.Body.Type);
}
[Fact]
public void ParseLambda_RealNumbers()
{
var parameters = new ParameterExpression[0];
var result1 = DynamicExpressionParser.ParseLambda(parameters, typeof(double), "0.10");
var result2 = DynamicExpressionParser.ParseLambda(parameters, typeof(double), "0.10d");
var result3 = DynamicExpressionParser.ParseLambda(parameters, typeof(float), "0.10f");
var result4 = DynamicExpressionParser.ParseLambda(parameters, typeof(decimal), "0.10m");
// Assert
Assert.Equal(0.10d, result1.Compile().DynamicInvoke());
Assert.Equal(0.10d, result2.Compile().DynamicInvoke());
Assert.Equal(0.10f, result3.Compile().DynamicInvoke());
Assert.Equal(0.10m, result4.Compile().DynamicInvoke());
}
[Fact]
public void ParseLambda_StringLiteral_ReturnsBooleanLambdaExpression()
{
var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"test\"");
Assert.Equal(typeof(Boolean), expression.Body.Type);
}
[Fact]
public void ParseLambda_StringLiteralEmpty_ReturnsBooleanLambdaExpression()
{
var expression = DynamicExpressionParser.ParseLambda(new[] { Expression.Parameter(typeof(string), "Property1") }, typeof(Boolean), "Property1 == \"\"");
Assert.Equal(typeof(Boolean), expression.Body.Type);
}
[Fact]
public void ParseLambda_StringLiteralEmbeddedQuote_ReturnsBooleanLambdaExpression()
{
string expectedRightValue = "\"test \\\"string\"";
var expression = DynamicExpressionParser.ParseLambda(
new[] { Expression.Parameter(typeof(string), "Property1") },
typeof(Boolean),
string.Format("Property1 == {0}", expectedRightValue));
string rightValue = ((BinaryExpression)expression.Body).Right.ToString();
Assert.Equal(typeof(Boolean), expression.Body.Type);
Assert.Equal(expectedRightValue, rightValue);
}
[Fact]
public void ParseLambda_StringLiteralStartEmbeddedQuote_ReturnsBooleanLambdaExpression()
{
string expectedRightValue = "\"\\\"test\"";
var expression = DynamicExpressionParser.ParseLambda(
new[] { Expression.Parameter(typeof(string), "Property1") },
typeof(Boolean),
string.Format("Property1 == {0}", expectedRightValue));
string rightValue = ((BinaryExpression)expression.Body).Right.ToString();
Assert.Equal(typeof(Boolean), expression.Body.Type);
Assert.Equal(expectedRightValue, rightValue);
}
[Fact]
public void ParseLambda_StringLiteral_MissingClosingQuote()
{
string expectedRightValue = "\"test\\\"";
Assert.Throws<ParseException>(() => DynamicExpressionParser.ParseLambda(
new[] { Expression.Parameter(typeof(string), "Property1") },
typeof(Boolean),
string.Format("Property1 == {0}", expectedRightValue)));
}
[Fact]
public void ParseLambda_StringLiteralEscapedBackslash_ReturnsBooleanLambdaExpression()
{
string expectedRightValue = "\"test\\string\"";
var expression = DynamicExpressionParser.ParseLambda(
new[] { Expression.Parameter(typeof(string), "Property1") },
typeof(Boolean),
string.Format("Property1 == {0}", expectedRightValue));
string rightValue = ((BinaryExpression)expression.Body).Right.ToString();
Assert.Equal(typeof(Boolean), expression.Body.Type);
Assert.Equal(expectedRightValue, rightValue);
}
[Fact]
public void ParseLambda_TupleToStringMethodCall_ReturnsStringLambdaExpression()
{
var expression = DynamicExpressionParser.ParseLambda(
typeof(Tuple<int>),
typeof(string),
"it.ToString()");
Assert.Equal(typeof(string), expression.ReturnType);
}
[Fact]
public void ParseLambda_IllegalMethodCall_ThrowsException()
{
Check.ThatCode(() =>
{
DynamicExpressionParser.ParseLambda(typeof(IO.FileStream), null, "it.Close()");
})
.Throws<ParseException>().WithMessage("Methods on type 'Stream' are not accessible");
}
}
}