[PT Run] Calculator plugin: Various improvements (#18159)

* crash fixes and error result

* small changes and test fixes

* improve exceptions and message

* double array crash fix

* overflowexception

* improve error handling

* varous improvements

* varous improvements

* fix spelling

* fix spelling

* Revert #16980

* add description

* error improvemenet

* Update tests

* spelling fixes

* small changes

* add settings

* last changes

* fix description

* update dev docs

* spell check
This commit is contained in:
Heiko
2022-06-02 11:44:12 +02:00
committed by GitHub
parent 9e4a58ee95
commit 465df35d27
13 changed files with 363 additions and 56 deletions

View File

@@ -32,21 +32,21 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
var engine = new CalculateEngine();
// Act
Assert.ThrowsException<ArgumentNullException>(() => engine.Interpret(input, CultureInfo.CurrentCulture));
Assert.ThrowsException<ArgumentNullException>(() => engine.Interpret(input, CultureInfo.CurrentCulture, out _));
}
[DataTestMethod]
[DataRow("42")]
[DataRow("test")]
[DataRow("pi(2)")] // Incorrect input, constant is being treated as a function.
[DataRow("e(2)")]
[DataRow("[10,10]")] // '[10,10]' is interpreted as array by mages engine
public void Interpret_NoResult_WhenCalled(string input)
{
// Arrange
var engine = new CalculateEngine();
// Act
var result = engine.Interpret(input, CultureInfo.CurrentCulture);
var result = engine.Interpret(input, CultureInfo.CurrentCulture, out _);
// Assert
Assert.AreEqual(default(CalculateResult), result);
@@ -87,7 +87,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Act
// Using InvariantCulture since this is internal
var result = engine.Interpret(input, CultureInfo.InvariantCulture);
var result = engine.Interpret(input, CultureInfo.InvariantCulture, out _);
// Assert
Assert.IsNotNull(result);
@@ -111,7 +111,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Act
// Using InvariantCulture since this is internal
var result = engine.Interpret(input, CultureInfo.InvariantCulture);
var result = engine.Interpret(input, CultureInfo.InvariantCulture, out _);
// Assert
Assert.IsNotNull(result);
@@ -135,7 +135,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
var engine = new CalculateEngine();
// Act
var result = engine.Interpret(input, cultureInfo);
var result = engine.Interpret(input, cultureInfo, out _);
// Assert
Assert.IsNotNull(result);
@@ -184,7 +184,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Act
// Using InvariantCulture since this is internal
var result = engine.Interpret(input, CultureInfo.InvariantCulture);
var result = engine.Interpret(input, CultureInfo.InvariantCulture, out _);
// Assert
Assert.IsNotNull(result);
@@ -210,7 +210,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Act
// Using InvariantCulture since this is internal
var result = engine.Interpret(input, CultureInfo.InvariantCulture);
var result = engine.Interpret(input, CultureInfo.InvariantCulture, out _);
// Assert
Assert.IsNotNull(result);

View File

@@ -8,9 +8,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />

View File

@@ -132,12 +132,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
}
[DataTestMethod]
[DataRow("12.0004", "12.0004")]
[DataRow("12,0004", "12.0004")]
[DataRow("0xF000", "0xF000")]
public void Translate_NoRemovalOfLeadingZeroesOnEdgeCases(string input, string expectedResult)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("pt-PT"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-de"), new CultureInfo("en-US"));
// Act
var result = translator.Translate(input);

View File

@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Wox.Plugin;
namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
{
[TestClass]
public class QueryTests
{
[DataTestMethod]
[DataRow("=pi(9+)", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=pi(9)", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=pi,", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=log()", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=0xf0x6", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=0xf,0x6", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=2^96", "Result value was either too large or too small for a decimal number")]
[DataRow("=+()", "Calculation result is not a valid number (NaN)")]
[DataRow("=[10,10]", "Unsupported use of square brackets")]
public void ErrorResultOnInvalidKeywordQuery(string typedString, string expectedResult)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
// Assert
Assert.AreEqual(expectedResult, result);
}
[DataTestMethod]
[DataRow("pi(9+)")]
[DataRow("pi(9)")]
[DataRow("pi,")]
[DataRow("log()")]
[DataRow("0xf0x6")]
[DataRow("0xf,0x6")]
[DataRow("2^96")]
[DataRow("+()")]
[DataRow("[10,10]")]
public void NoResultOnInvalidGlobalQuery(string typedString)
{
// Setup
Mock<Main> main = new ();
Query expectedQuery = new (typedString);
// Act
var result = main.Object.Query(expectedQuery).Count;
// Assert
Assert.AreEqual(result, 0);
}
}
}