From f88157d1a074520ee9e88650333a708b36c22c1f Mon Sep 17 00:00:00 2001 From: HZY Date: Sat, 27 Jan 2024 14:55:58 +0800 Subject: [PATCH] Update LambdaParserTests.cs https://github.com/nreco/lambdaparser/issues/3#issuecomment-1912173889 --- .../LambdaParserTests.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/NReco.LambdaParser.Tests/LambdaParserTests.cs b/src/NReco.LambdaParser.Tests/LambdaParserTests.cs index 6acc4ca..33b364d 100644 --- a/src/NReco.LambdaParser.Tests/LambdaParserTests.cs +++ b/src/NReco.LambdaParser.Tests/LambdaParserTests.cs @@ -216,6 +216,34 @@ public void EvalCachePerf() { Console.WriteLine("10000 iterations: {0}", sw.Elapsed); } + [Fact] + public void MultiParameterFunc() + { + var lambdaParser = new LambdaParser(new OptionsParamsInvokeMethod()); + + var context = getContext(); + context["f"] = new Funcs(); + context["sum"] = new Func((p) => + { + if (p is ICollection collection) + { + return collection.Cast().Select(w => + { + decimal.TryParse(w?.ToString(), out var value); + return value; + })?.Sum() ?? 0; + } + + decimal.TryParse(p?.ToString(), out var value1); + return value1; + }); + + var value1 = ; // + + Assert.Equal(13M, + lambdaParser.Eval("sum(1,2,3) + sum(1) + f.sum(1,2,3)", context)); + } + public class TestBaseClass { @@ -295,5 +323,24 @@ public string Format(string format,params object[] args) } + + public class Funcs + { + public decimal sum(params object[] p) + { + if (p is ICollection collection) + { + return collection.Cast().Select(w => + { + decimal.TryParse(w?.ToString(), out var value); + return value; + })?.Sum() ?? 0; + } + + return 0; + } + } + + } }