-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDynamicLinqTests.cs
47 lines (41 loc) · 1.82 KB
/
DynamicLinqTests.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
using System.Linq;
using Xunit;
using System.Linq.Dynamic.Core;
using ZSpitz.Util;
using System.Linq.Expressions;
using static ExpressionTreeToString.BuiltinRenderer;
using static System.Linq.Expressions.Expression;
using System.Linq.Dynamic.Core.Parser;
using static ExpressionTreeToString.Tests.Globals;
using System;
using static ZSpitz.Util.Functions;
namespace ExpressionTreeToString.Tests {
public class DynamicLinqTests {
public static TheoryData<string, string, Expression> TestData =>
Objects
.Where(x => x.source == nameof(DynamicLinqTestObjects) && x.o is Expression)
.SelectT((category, source, name, o) => (category, name, (Expression)o))
.ToTheoryData();
[Theory]
[MemberData(nameof(TestData))]
public void TestMethod(string category, string name, Expression expr) {
var selector = expr.ToString(DynamicLinq, "C#");
if (category == "_Unrepresentable") {
Assert.True(selector?.Contains("Not implemented"));
return;
}
var dynamicLinqParameters = EmptyArray<object>();
var parts = selector.Split(new[] { "\r\n\r\n" }, default);
if (parts.Length>1) {
selector = parts.Last();
dynamicLinqParameters = DynamicLinqTestObjects.Parameters[name];
}
if (expr is not LambdaExpression) { return; }
selector = selector[1..^1].Replace("\\\"", "\"").Replace("\\'", "'").Replace("\\\\", "\\");
var prm = Parameter(typeof(Person));
var parser = new ExpressionParser(new[] { prm }, selector, dynamicLinqParameters, ParsingConfig.Default);
// test that the generated string can be parsed successfully
var _ = parser.Parse(null);
}
}
}