Adjusting to force all instances to use rounded vs result due to Mage's quirks (#7164)

This commit is contained in:
Clint Rutkas
2020-10-08 08:57:17 -07:00
committed by GitHub
parent cfe2bbd75e
commit 9928579364
4 changed files with 21 additions and 11 deletions

View File

@@ -53,12 +53,14 @@ namespace Microsoft.Plugin.Calculator.UnitTests
[TestCase("round(2 * pi)", 6D)]
[TestCase("1 == 2", default(double))]
[TestCase("pi * ( sin ( cos ( 2)))", -1.26995475603563D)]
[TestCase("5.6/2", 2.8D)]
[TestCase("5.6/2", 2.8D)]
[TestCase("123 * 4.56", 560.88D)]
[TestCase("1 - 9.0 / 10", 0.1D)]
[TestCase("0.5 * ((2*-395.2)+198.2)", -296.1D)]
[TestCase("2+2.11", 4.11D)]
public void Interpret_NoErrors_WhenCalled(string input, decimal expectedResult)
[TestCase("8.43 + 4.43 - 12.86", 0D)]
[TestCase("8.43 + 4.43 - 12.8", 0.06D)]
public void Interpret_NoErrors_WhenCalledWithRounding(string input, decimal expectedResult)
{
// Arrange
var engine = new CalculateEngine();
@@ -68,7 +70,7 @@ namespace Microsoft.Plugin.Calculator.UnitTests
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expectedResult, result.Result);
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
}
[TestCase("0.100000000000000000000", 0.00776627963145224D)] // BUG: Because data structure
@@ -101,7 +103,7 @@ namespace Microsoft.Plugin.Calculator.UnitTests
// Assert
Assert.IsNotNull(result);
Assert.AreEqual(expectedResult, result.Result);
Assert.AreEqual(CalculateEngine.Round(expectedResult), result.RoundedResult);
}
[TestCase("ceil(2 * (pi ^ 2))", true)]

View File

@@ -41,7 +41,7 @@ namespace Microsoft.Plugin.Calculator
}
var decimalResult = Convert.ToDecimal(result, cultureInfo);
var roundedResult = Math.Round(decimalResult, RoundingDigits, MidpointRounding.AwayFromZero);
var roundedResult = Round(decimalResult);
return new CalculateResult()
{
@@ -50,6 +50,11 @@ namespace Microsoft.Plugin.Calculator
};
}
public static decimal Round(decimal value)
{
return Math.Round(value, RoundingDigits, MidpointRounding.AwayFromZero);
}
private static object TransformResult(object result)
{
if (result.ToString() == "NaN")

View File

@@ -44,7 +44,7 @@ namespace Microsoft.Plugin.Calculator
return new List<Result>
{
ResultHelper.CreateResult(result.Result, result.RoundedResult, IconPath),
ResultHelper.CreateResult(result.RoundedResult, IconPath),
};
} // We want to keep the process alive if any the mages library throws any exceptions.
#pragma warning disable CA1031 // Do not catch general exception types

View File

@@ -14,10 +14,10 @@ namespace Microsoft.Plugin.Calculator
{
public static Result CreateResult(CalculateResult result, string iconPath)
{
return CreateResult(result.Result, result.RoundedResult, iconPath);
return CreateResult(result.RoundedResult, iconPath);
}
public static Result CreateResult(decimal result, decimal roundedResult, string iconPath)
public static Result CreateResult(decimal roundedResult, string iconPath)
{
return new Result
{
@@ -25,18 +25,19 @@ namespace Microsoft.Plugin.Calculator
IcoPath = iconPath,
Score = 300,
SubTitle = Properties.Resources.wox_plugin_calculator_copy_number_to_clipboard,
Action = c => Action(result),
Action = c => Action(roundedResult),
};
}
public static bool Action(decimal result)
public static bool Action(decimal roundedResult)
{
var ret = false;
var thread = new Thread(() =>
{
try
{
Clipboard.SetText(result.ToString(CultureInfo.CurrentUICulture.NumberFormat));
Clipboard.SetText(roundedResult.ToString(CultureInfo.CurrentUICulture.NumberFormat));
ret = true;
}
catch (ExternalException)
@@ -44,9 +45,11 @@ namespace Microsoft.Plugin.Calculator
MessageBox.Show(Properties.Resources.wox_plugin_calculator_copy_failed);
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ret;
}
}