Skip to content

Commit

Permalink
fix ambiguous method bug mparlak#84
Browse files Browse the repository at this point in the history
  • Loading branch information
hunkydoryrepair committed Mar 26, 2021
1 parent 09c78f1 commit b5d343f
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Flee.Net45/InternalTypes/Miscellaneous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Flee.NetStandard20/InternalTypes/Miscellaneous.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/Flee.Console/Flee.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Flee" Version="1.2.1" />
<ProjectReference Include="..\..\src\Flee.NetStandard20\Flee.NetStandard20.csproj" />
</ItemGroup>

</Project>
75 changes: 73 additions & 2 deletions test/Flee.Test/CalcEngineTests/LongScriptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}



}
}
17 changes: 17 additions & 0 deletions test/Flee.Test/ExtensionMethodTests/ExtensionMethodTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ public void TestExtensionMethodCallOnPropertyWithArgumentsOnOverload()
Assert.AreEqual("Hello SubWorld!!!", result);
}

/// <summary>
/// check that methods are not ambiguous.
/// </summary>
[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" };
Expand Down
26 changes: 26 additions & 0 deletions test/Flee.Test/ExtensionMethodTests/TestData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,31 @@ public string SayHello(int times)

return result + Id;
}

/// <summary>
/// A bug previous meant a small difference in
/// parameters was not detected and treated
/// as ambiguous
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="z"></param>
/// <returns></returns>
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";
}
}
}
10 changes: 7 additions & 3 deletions test/Flee.Test/Flee.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Flee.Net45, Version=1.0.0.0, Culture=neutral, PublicKeyToken=951a102ce2413032, processorArchitecture=MSIL">
<HintPath>..\..\packages\Flee.1.2.1\lib\net45\Flee.Net45.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\packages\MSTest.TestFramework.1.1.11\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -72,6 +69,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="TestScripts\NestedConditionals.js" />
<Content Include="TestScripts\readme.txt" />
<Content Include="TestScripts\CheckedTests.txt" />
<Content Include="TestScripts\IndividualTests.xml" />
Expand All @@ -83,6 +81,12 @@
<Content Include="TestScripts\ValidCasts.txt" />
<Content Include="TestScripts\ValidExpressions.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Flee.Net45\Flee.Net45.csproj">
<Project>{658c0ed4-404a-48ec-96ff-d22c3c12ac39}</Project>
<Name>Flee.Net45</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down

0 comments on commit b5d343f

Please sign in to comment.