From b5d343f2e729c4503016a00d509f35446517e35a Mon Sep 17 00:00:00 2001 From: Garr Godfrey Date: Thu, 25 Mar 2021 20:21:41 -0700 Subject: [PATCH] fix ambiguous method bug #84 --- src/Flee.Net45/InternalTypes/Miscellaneous.cs | 2 +- .../InternalTypes/Miscellaneous.cs | 2 +- test/Flee.Console/Flee.Console.csproj | 2 +- .../CalcEngineTests/LongScriptTests.cs | 75 ++++++++++++++++++- .../ExtensionMethodTest.cs | 17 +++++ .../ExtensionMethodTests/TestData.cs | 26 +++++++ test/Flee.Test/Flee.Test.csproj | 10 ++- 7 files changed, 126 insertions(+), 8 deletions(-) diff --git a/src/Flee.Net45/InternalTypes/Miscellaneous.cs b/src/Flee.Net45/InternalTypes/Miscellaneous.cs index 7646efd..95f1aae 100644 --- a/src/Flee.Net45/InternalTypes/Miscellaneous.cs +++ b/src/Flee.Net45/InternalTypes/Miscellaneous.cs @@ -238,7 +238,7 @@ private float ComputeScoreInternal(ParameterInfo[] parameters, Type[] argTypes) // Our score is the average of the scores of each parameter. The lower the score, the better the match. int sum = ComputeSum(parameters, argTypes); - return sum / argTypes.Length; + return (float)sum / (float)argTypes.Length; } private static int ComputeSum(ParameterInfo[] parameters, Type[] argTypes) diff --git a/src/Flee.NetStandard20/InternalTypes/Miscellaneous.cs b/src/Flee.NetStandard20/InternalTypes/Miscellaneous.cs index a5fbbcd..4d1595c 100644 --- a/src/Flee.NetStandard20/InternalTypes/Miscellaneous.cs +++ b/src/Flee.NetStandard20/InternalTypes/Miscellaneous.cs @@ -238,7 +238,7 @@ private float ComputeScoreInternal(ParameterInfo[] parameters, Type[] argTypes) // Our score is the average of the scores of each parameter. The lower the score, the better the match. int sum = ComputeSum(parameters, argTypes); - return sum / argTypes.Length; + return (float)sum / (float)argTypes.Length; } private static int ComputeSum(ParameterInfo[] parameters, Type[] argTypes) diff --git a/test/Flee.Console/Flee.Console.csproj b/test/Flee.Console/Flee.Console.csproj index 3445e9e..ea62205 100644 --- a/test/Flee.Console/Flee.Console.csproj +++ b/test/Flee.Console/Flee.Console.csproj @@ -6,7 +6,7 @@ - + diff --git a/test/Flee.Test/CalcEngineTests/LongScriptTests.cs b/test/Flee.Test/CalcEngineTests/LongScriptTests.cs index 22d5a0a..72cfcf1 100644 --- a/test/Flee.Test/CalcEngineTests/LongScriptTests.cs +++ b/test/Flee.Test/CalcEngineTests/LongScriptTests.cs @@ -30,7 +30,22 @@ public LongScriptTests() [Test] public void LongScriptWithManyFunctions() { - var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\LongScriptWithManyFunctions.js"); + //var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\LongScriptWithManyFunctions.js"); + var script = @"If((""LongTextToPushScriptLengthOver256CharactersJustToMakeSureItDoesntMatter"")=""C"", + ( + If((""D"") = ""E"", + 2, + 3 + ) + ), + ( + If((""D"") = ""E"", + Ceiling((((((4000) / 46.228) + 8) * 46.228) * 3) + 5126) + 1471 / 304.8 + 20, + Ceiling(((((((((4000) / 46.228) + 8) * 46.228) * 3) + 5126) + 1217) / 304.8) + 20) + ) + ) +)"; + var expr = _myEngine.Context.CompileDynamic(script); var result = expr.Evaluate(); @@ -41,11 +56,67 @@ public void LongScriptWithManyFunctions() [Test] public void FailingLongScriptWithManyFunctions() { - var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\FailingLongScriptWithManyFunctions.js"); + //var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\FailingLongScriptWithManyFunctions.js"); + var script = @" +If(""A"" = ""A"", + ( + If((""LongTextToPushScriptLengthOver256CharactersJustToMakeSureItDoesntMatter"") = ""C"", + ( + If((""D"") = ""E"", + 2, + 3 + ) + ), + ( + If((""D"") = ""E"", + Ceiling((((((4000) / 46.228) + 8) * 46.228) * 3) + 5126) + 1471 / 304.8 + 20, + Ceiling(((((((((4000) / 46.228) + 8) * 46.228) * 3) + 5126) + 1217) / 304.8) + 20) + ) + ) + ) + ),0 + ) +"; var expr = _myEngine.Context.CompileDynamic(script); var result = expr.Evaluate(); Assert.AreEqual(84.0d, result); } + + [Test] + public void NestedConditionalsForLongBranches() + { + //var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\NestedConditionals.js"); + var script = @"IF(2.1 <> 2.1, +IF(2.1 > 2.1, 2.1, +IF(2.1 > 2.1 AND 2.1 <= 2.1, 2.1, +IF(2.1 > 2.1 AND 2.1 <= 2.1, 2.1, 2.1))), +IF(2.1 > 2.1, 2.1, +IF(2.1 > 2.1 AND 2.1 <= 2.1, 2.1, +IF(2.1 > 2.1 AND 2.1 <= 2.1, 2.1, 2.1))))"; + var expr = _myEngine.Context.CompileDynamic(script); + var result = expr.Evaluate(); + + Assert.AreEqual(2.1d, Convert.ToDecimal(result)); + } + + [Test] + public void ShortCircuitLongBranches() + { + //var script = System.IO.File.ReadAllText(@"test\Flee.Test\TestScripts\NestedConditionals.js"); + var script = @"IF( +1 = 2 AND (16 * 24 + 8 * -1 < 0 OR 1+1+1+1+1+1+1+1+1+1+1+1+2+3+4+5+6+7+8+9+1+2+3+4+5+6+7+8+9+1+2+3+4+5+6+7+8+9+1+2+3*3-900 < 0) +AND (5*6+13-6*9-3+1+2+3+4+5+6+7+8 = 5+6+7+8+9+1+2+3+4+5+6+1+2+3+4+9-48 OR 6+5+2+3+8+1*9-6*7 > 8+6*4*(15-6)*(5+1+1+1+1+1+1+1+2)) +, +1.4d,2.6d +)"; + var expr = _myEngine.Context.CompileDynamic(script); + var result = expr.Evaluate(); + + Assert.AreEqual(2.6d, result); + } + + + } } diff --git a/test/Flee.Test/ExtensionMethodTests/ExtensionMethodTest.cs b/test/Flee.Test/ExtensionMethodTests/ExtensionMethodTest.cs index 76f71de..ce1d1e7 100644 --- a/test/Flee.Test/ExtensionMethodTests/ExtensionMethodTest.cs +++ b/test/Flee.Test/ExtensionMethodTests/ExtensionMethodTest.cs @@ -65,6 +65,23 @@ public void TestExtensionMethodCallOnPropertyWithArgumentsOnOverload() Assert.AreEqual("Hello SubWorld!!!", result); } + /// + /// check that methods are not ambiguous. + /// + [TestMethod] + public void TestExtensionMethodMatchArguments() + { + var result = GetExpressionContext().CompileDynamic("MatchParams(1, 2.3f, 2.3)").Evaluate(); + Assert.AreEqual("FFD", result); + result = GetExpressionContext().CompileDynamic("MatchParams(3.4,4.4,2.3)").Evaluate(); + Assert.AreEqual("DDD", result); + result = GetExpressionContext().CompileDynamic("MatchParams(1,2,3)").Evaluate(); + Assert.AreEqual("III", result); + result = GetExpressionContext().CompileDynamic("MatchParams(1u,2,3)").Evaluate(); + Assert.AreEqual("UII", result); + } + + private static ExpressionContext GetExpressionContext() { var expressionOwner = new TestData { Id = "World" }; diff --git a/test/Flee.Test/ExtensionMethodTests/TestData.cs b/test/Flee.Test/ExtensionMethodTests/TestData.cs index d0202cd..830f50b 100644 --- a/test/Flee.Test/ExtensionMethodTests/TestData.cs +++ b/test/Flee.Test/ExtensionMethodTests/TestData.cs @@ -21,5 +21,31 @@ public string SayHello(int times) return result + Id; } + + /// + /// A bug previous meant a small difference in + /// parameters was not detected and treated + /// as ambiguous + /// + /// + /// + /// + /// + public string MatchParams(uint x, int y, int z) + { + return "UII"; + } + public string MatchParams(int x, int y, int z) + { + return "III"; + } + public string MatchParams(float x, float y, double z) + { + return "FFD"; + } + public string MatchParams(double x, double y, double z) + { + return "DDD"; + } } } diff --git a/test/Flee.Test/Flee.Test.csproj b/test/Flee.Test/Flee.Test.csproj index dbbb54a..c4fb2ba 100644 --- a/test/Flee.Test/Flee.Test.csproj +++ b/test/Flee.Test/Flee.Test.csproj @@ -39,9 +39,6 @@ 4 - - ..\..\packages\Flee.1.2.1\lib\net45\Flee.Net45.dll - ..\..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll @@ -72,6 +69,7 @@ + @@ -83,6 +81,12 @@ + + + {658c0ed4-404a-48ec-96ff-d22c3c12ac39} + Flee.Net45 + +