-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathFormulaEvaluatorTests.cs
More file actions
196 lines (174 loc) · 7.04 KB
/
Copy pathFormulaEvaluatorTests.cs
File metadata and controls
196 lines (174 loc) · 7.04 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
using ClosedXML.Report.Utils;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.CustomTypeProviders;
using System.Linq.Dynamic.Core.Exceptions;
using System.Linq.Expressions;
using FluentAssertions;
using Xunit;
namespace ClosedXML.Report.Tests
{
public class FormulaEvaluatorTests
{
[Fact]
public void FormulaEvaluatorTests1()
{
var eval = new FormulaEvaluator();
eval.AddVariable("a", 2);
eval.AddVariable("b", 3);
eval.Evaluate("{{\"test\"}}").Should().Be("test");
eval.Evaluate("{{a+b}}").Should().Be(5);
eval.Evaluate("{{c}}+{{d}}={{c+d}}", new Parameter("c", 3), new Parameter("d", 6)).Should().Be("3+6=9");
eval.Evaluate("{{c}}+{{d}}={{c+d}}", new Parameter("c", 7), new Parameter("d", 8)).Should().Be("7+8=15");
}
[Fact]
public void ExpressionParseTest()
{
var customers = new Customer[]
{
new Customer {Id = 1, Name = "Customer1"},
new Customer {Id = 2, Name = "Customer2"}
}.AsEnumerable();
string query = "customers.Where(c => c.Id == 1).OrderBy(c=> c.Name)";
var lambda = XLDynamicExpressionParser.ParseLambda(new [] {Expression.Parameter(customers.GetType(), "customers")}, null, query);
var dlg = lambda.Compile();
dlg.DynamicInvoke(customers).Should().BeAssignableTo<IEnumerable<Customer>>();
((IEnumerable<Customer>) dlg.DynamicInvoke(customers)).Should().HaveCount(1);
((IEnumerable<Customer>) dlg.DynamicInvoke(customers)).First().Id.Should().Be(1);
}
[Fact]
public void MultipleExpressionsWithNullResult()
{
var eval = new FormulaEvaluator();
eval.AddVariable("a", null);
eval.AddVariable("b", 1);
eval.Evaluate("{{a}}{{b}}").Should().Be(1);
eval.Evaluate("{{b}}{{a}}").Should().Be("1");
}
[Theory,
InlineData("{{\"Hello \"+a}}","Hello "),
InlineData("{{\"City: \"+Iif(a==null, string.Empty, a.City)}}","City: ")
]
public void PassNullParameter(string formula, object expected)
{
var eval = new FormulaEvaluator();
eval.Evaluate(formula, new Parameter("a", null)).Should().Be(expected);
}
[Fact]
public void WrongExpressionShouldThrowParseException()
{
var eval = new FormulaEvaluator();
Assert.Throws<ParseException>(() => eval.Evaluate("{{missing}}"));
}
[Fact]
public void ParseExceptionMessageShouldBeUnknownIdentifier()
{
var eval = new FormulaEvaluator();
Assert.Throws<ParseException>(() => eval.Evaluate("{{item.id}}"))
.Message.Should().Be("Unknown identifier 'item'");
}
[Fact]
public void EvalExpressionVariableWithAt()
{
var eval = new FormulaEvaluator();
eval.AddVariable("@a", 1);
eval.Evaluate("{{@a+@a}}").Should().Be(2);
}
[Fact]
public void ExpressionParseTestNullPropagation()
{
var customers = new[]
{
new Customer {Id = 1, Name = "Customer1", Manager = new Customer { Id = 3, Name = "Manager1"}},
new Customer {Id = 2, Name = "Customer2", Manager = null}
};
var eval = new FormulaEvaluator();
eval.AddVariable("a", customers[0]);
eval.AddVariable("b", customers[1]);
eval.Evaluate(@"{{np(a.Manager.Name, ""test"")}}").Should().Be("Manager1");
eval.Evaluate(@"{{np(b.Manager.Name, ""test"")}}").Should().Be("test");
eval.Evaluate(@"{{np(b.Manager.Name, null)}}").Should().BeNull();
}
[Fact]
public void EvalDictionaryParams()
{
Parameter CreateDicParameter(string name) => new Parameter("item", new Dictionary<string, object>
{{"Name", new Dictionary<string, object> {{"FirstName", name }}}});
var eval = new FormulaEvaluator();
eval.Evaluate("{{item.Name.FirstName}}", CreateDicParameter("Julio")).Should().Be("Julio");
eval.Evaluate("{{item.Name.FirstName}}", CreateDicParameter("John")).Should().Be("John");
}
[Fact]
public void EvalDictionaryParams2()
{
object CreateDicParameter(string name) => new Dictionary<string, object>
{{"Name", new Dictionary<string, object> {{"FirstName", name }}}};
var config = new ParsingConfig();
config.CustomTypeProvider = new DefaultDynamicLinqCustomTypeProvider(config, cacheCustomTypes: true);
var parType = new Dictionary<string, object>().GetType();
var lambda = DynamicExpressionParser.ParseLambda(config, new [] {Expression.Parameter(parType, "item")}, typeof(object), "item.Name.FirstName").Compile();
lambda.DynamicInvoke(CreateDicParameter("Julio")).Should().Be("Julio");
lambda.DynamicInvoke(CreateDicParameter("John")).Should().Be("John");
}
[Fact]
public void UsingDynamicLinqTypeTest()
{
var eval = new FormulaEvaluator();
eval.AddVariable("a", "1");
eval.Evaluate("{{EvaluateUtils.ParseAsInt(a).IncrementMe()}}").Should().Be(2);
}
[Fact]
public void EvalMixedArray()
{
var mixed = new object[] {
"string",
1,
0.1,
System.DateTime.Today,
};
var eval = new FormulaEvaluator();
eval.AddVariable("mixed", mixed);
eval.Evaluate("{{mixed.Count()}}").Should().Be(4);
foreach (var item in mixed)
{
eval.Evaluate("{{item}}", new Parameter("item", item));
}
}
[Fact]
public void UsingLambdaExpressionsIssue212()
{
var customers = new object[10000];
for (int i = 0; i < customers.Length; i++)
{
customers[i] = new Customer { Id = i, Name = "Customer"+i };
}
var eval = new FormulaEvaluator();
eval.AddVariable("items", customers);
eval.Evaluate("{{items.Count(c => c.Id == 9999)}}").Should().Be(1);
eval.Evaluate("{{items.Select(i => i.Name).Skip(1000).First()}}").Should().Be("Customer1000");
}
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Customer Manager { get; set; }
}
}
[DynamicLinqType]
public static class EvaluateUtils
{
public static int ParseAsInt(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value);
}
public static int IncrementMe(this int values)
{
return values + 1;
}
}
}