|
1 | 1 | using ClosedXML.Report.Utils;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Linq;
|
| 4 | +using System.Linq.Dynamic.Core; |
| 5 | +using System.Linq.Dynamic.Core.CustomTypeProviders; |
4 | 6 | using System.Linq.Dynamic.Core.Exceptions;
|
5 | 7 | using System.Linq.Expressions;
|
6 | 8 | using FluentAssertions;
|
@@ -97,11 +99,65 @@ public void ExpressionParseTestNullPropagation()
|
97 | 99 | eval.Evaluate(@"{{np(b.Manager.Name, null)}}").Should().BeNull();
|
98 | 100 | }
|
99 | 101 |
|
| 102 | + [Fact(Skip = "It doesn't work yet")] |
| 103 | + public void EvalDictionaryParams() |
| 104 | + { |
| 105 | + Parameter CreateDicParameter(string name) => new Parameter("item", new Dictionary<string, object> |
| 106 | + {{"Name", new Dictionary<string, object> {{"FirstName", name }}}}); |
| 107 | + |
| 108 | + var eval = new FormulaEvaluator(); |
| 109 | + eval.Evaluate("{{item.Name.FirstName}}", CreateDicParameter("Julio")).Should().Be("Julio"); |
| 110 | + eval.Evaluate("{{item.Name.FirstName}}", CreateDicParameter("John")).Should().Be("John"); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact(Skip = "It doesn't work yet")] |
| 114 | + public void EvalDictionaryParams2() |
| 115 | + { |
| 116 | + object CreateDicParameter(string name) => new Dictionary<string, object> |
| 117 | + {{"Name", new Dictionary<string, object> {{"FirstName", name }}}}; |
| 118 | + |
| 119 | + var config = new ParsingConfig() |
| 120 | + { |
| 121 | + CustomTypeProvider = new DefaultDynamicLinqCustomTypeProvider() |
| 122 | + }; |
| 123 | + var parType = new Dictionary<string, object>().GetType(); |
| 124 | + var lambda = DynamicExpressionParser.ParseLambda(config, new [] {Expression.Parameter(parType, "item")}, typeof(object), "item.Name.FirstName").Compile(); |
| 125 | + lambda.DynamicInvoke(CreateDicParameter("Julio")).Should().Be("Julio"); |
| 126 | + lambda.DynamicInvoke(CreateDicParameter("John")).Should().Be("John"); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void UsingDynamicLinqTypeTest() |
| 131 | + { |
| 132 | + var eval = new FormulaEvaluator(); |
| 133 | + eval.AddVariable("a", "1"); |
| 134 | + eval.Evaluate("{{EvaluateUtils.ParseAsInt(a).IncrementMe()}}").Should().Be(2); |
| 135 | + } |
| 136 | + |
100 | 137 | class Customer
|
101 | 138 | {
|
102 | 139 | public int Id { get; set; }
|
103 | 140 | public string Name { get; set; }
|
104 | 141 | public Customer Manager { get; set; }
|
105 | 142 | }
|
106 | 143 | }
|
| 144 | + |
| 145 | + [DynamicLinqType] |
| 146 | + public static class EvaluateUtils |
| 147 | + { |
| 148 | + public static int ParseAsInt(string value) |
| 149 | + { |
| 150 | + if (value == null) |
| 151 | + { |
| 152 | + return 0; |
| 153 | + } |
| 154 | + |
| 155 | + return int.Parse(value); |
| 156 | + } |
| 157 | + |
| 158 | + public static int IncrementMe(this int values) |
| 159 | + { |
| 160 | + return values + 1; |
| 161 | + } |
| 162 | + } |
107 | 163 | }
|
0 commit comments