Skip to content

Commit

Permalink
No longer stop parsing an expression after invoking a custom function #…
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi committed Jun 7, 2021
2 parents f505bd9 + 90cce49 commit 6dfa187
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,8 @@ private Expression ParsePrimary()
return ParseLambdaInvocation(lambda, tokenPos);

if (expr is MethodGroupExpression methodGroup)
return ParseMethodGroupInvocation(methodGroup, tokenPos);

if (typeof(Delegate).IsAssignableFrom(expr.Type))
expr = ParseMethodGroupInvocation(methodGroup, tokenPos);
else if (typeof(Delegate).IsAssignableFrom(expr.Type))
expr = ParseDelegateInvocation(expr, tokenPos);
else
throw CreateParseException(tokenPos, ErrorMessages.InvalidMethodCall, GetTypeName(expr.Type));
Expand Down
29 changes: 29 additions & 0 deletions test/DynamicExpresso.UnitTest/FunctionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using NUnit.Framework;

namespace DynamicExpresso.UnitTest
{
[TestFixture]
public class FunctionTest
{
[Test]
public void Calling_Function()
{
var target = new Interpreter();
Func<double, double, double> pow = (x, y) => Math.Pow(x, y);
target.SetFunction("pow", pow);

Assert.AreEqual(25, target.Eval("pow(5, 2)"));
}

[Test]
public void Chaining_Functions()
{
var target = new Interpreter();
Func<double, double, double> pow = (x, y) => Math.Pow(x, y);
target.SetFunction("pow", pow);

Assert.AreEqual("25", target.Eval("pow(5, 2).ToString()"));
}
}
}
13 changes: 13 additions & 0 deletions test/DynamicExpresso.UnitTest/GithubIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ public void GitHub_Issue_144_1()
Assert.False((bool)interpreter.Eval("GFunction()"));
}

[Test]
public void GitHub_Issue_148()
{
Func<object[], double, double, object[]> subArray = (entries, skipFirst, skipLast) => entries.Take(entries.Length - (int)skipLast).Skip((int)skipFirst).ToArray();

var target = new Interpreter();

target.SetVariable("arr1", new object[] { 1d, 2d, 3d });
target.SetFunction("SubArray", subArray);

Assert.AreEqual(2, target.Eval("SubArray(arr1, 1, 1).First()"));
}


#if NETCOREAPP2_1_OR_GREATER

Expand Down
11 changes: 11 additions & 0 deletions test/DynamicExpresso.UnitTest/MemberInvocationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ public void Method_with_optional_null_param()
Assert.AreEqual(x.MethodWithOptionalNullParam(y, z), target.Eval("x.MethodWithOptionalNullParam(y, z)", parameters));
}

[Test]
public void Chaining_Methods()
{
var x = new MyTestService();

var target = new Interpreter();
target.SetVariable("x", x);

Assert.AreEqual(x.HelloWorld().ToUpper(), target.Eval("x.HelloWorld().ToUpper()"));
}

private interface MyTestInterface
{
}
Expand Down

0 comments on commit 6dfa187

Please sign in to comment.