Skip to content

Commit

Permalink
Added support for invoking two expressions with different parameter c…
Browse files Browse the repository at this point in the history
…ount at the same time.
  • Loading branch information
Giorgi committed Sep 27, 2015
1 parent 4222739 commit f463ceb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 17 additions & 0 deletions SimpleExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,22 @@ public void Can_ParseNumbers_InDifferentCulture()

Assert.That(engine.Evaluate("5 000,67"), Is.EqualTo(5000.67));
}

[Test]
public void Can_Invoke_Two_Distinct_Expressions_With_Different_Parameters_Count()
{
var a = 6m;
var b = 3.9m;
var c = 4.9m;

var compiled = engine.Compile("(a+b)/(a+c)");
var compiled2 = engine.Compile("(a+b)/a");
Assert.That(compiled(new { a, b, c }), Is.EqualTo((a + b) / (a + c)));

a = 5.4m;
b = -2.4m;

Assert.That(compiled2(new { a, b }), Is.EqualTo((a + b) / a));
}
}
}
10 changes: 6 additions & 4 deletions SimpleExpressionEvaluator/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public class ExpressionEvaluator : DynamicObject
{
var compiled = Parse(expression);

Func<object, decimal> result = argument =>
Func<List<string>, Func<object, decimal>> curriedResult = list => argument =>
{
var arguments = ParseArguments(argument);
return Execute(compiled, arguments);
return Execute(compiled, arguments, list);
};

var result = curriedResult(parameters.ToList());

return result;
}

Expand Down Expand Up @@ -184,10 +186,10 @@ private decimal Evaluate(string expression, Dictionary<string, decimal> argument
{
var compiled = Parse(expression);

return Execute(compiled, arguments);
return Execute(compiled, arguments, parameters);
}

private decimal Execute(Func<decimal[], decimal> compiled, Dictionary<string, decimal> arguments)
private decimal Execute(Func<decimal[], decimal> compiled, Dictionary<string, decimal> arguments, List<string> parameters)
{
arguments = arguments ?? new Dictionary<string, decimal>();

Expand Down

0 comments on commit f463ceb

Please sign in to comment.