Skip to content

Commit

Permalink
Merge pull request Giorgi#12 from akinsgre/master
Browse files Browse the repository at this point in the history
Giorgi#11 - Making method signature public
  • Loading branch information
Giorgi committed Feb 27, 2024
2 parents 17c9594 + c82f964 commit 382680b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
16 changes: 16 additions & 0 deletions SimpleExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using NUnit.Framework;
Expand Down Expand Up @@ -125,6 +126,21 @@ public void Can_Pass_Named_Variables()

Assert.That(dynamicEngine.Evaluate("(c+b)*a", a: 6, b: 4.5, c: 2.6), Is.EqualTo((c + b) * a));
}
[Test]
public void Can_Pass_A_Dictionary()
{
dynamic dynamicEngine = new ExpressionEvaluator();

var a = 6;
var b = 4.5m;
var c = 2.6m;
Dictionary<String, decimal> dict = new Dictionary<string, decimal>();
dict.Add("a", a);
dict.Add("b", b);
dict.Add("c", c);

Assert.That(dynamicEngine.Evaluate("(c+b)*a", dict), Is.EqualTo((c + b) * a));
}

[Test]
public void Can_Invoke_Expression_Multiple_Times()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<ItemGroup>
<ProjectReference Include="..\SimpleExpressionEvaluator\SimpleExpressionEvaluator.csproj" />
</ItemGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
20 changes: 13 additions & 7 deletions SimpleExpressionEvaluator/ExpressionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o

return true;
}
/// <summary>
/// Parses and evaluates an expression with a Dictionary of arguments
/// </summary>
/// <param name="expression">Expression to parse</param>
/// <param name="arguments">An Dictionary object containing arguments for the expression
/// The key pairs matching the variable names in the expression</param>
/// <returns></returns>
public decimal Evaluate(string expression, Dictionary<string, decimal> arguments)
{
var compiled = Parse(expression);

return Execute(compiled, arguments, parameters);
}

private Func<decimal[], decimal> Parse(string expression)
{
Expand Down Expand Up @@ -220,13 +232,7 @@ public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, o

return arguments;
}

private decimal Evaluate(string expression, Dictionary<string, decimal> arguments)
{
var compiled = Parse(expression);

return Execute(compiled, arguments, parameters);
}


private decimal Execute(Func<decimal[], decimal> compiled, Dictionary<string, decimal> arguments, List<string> parameters)
{
Expand Down

0 comments on commit 382680b

Please sign in to comment.