-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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
- Loading branch information
Showing
13 changed files
with
363 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/ErrorHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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; | ||
using System.Collections.Generic; | ||
using Wox.Plugin; | ||
using Wox.Plugin.Logger; | ||
|
||
namespace Microsoft.PowerToys.Run.Plugin.Calculator | ||
{ | ||
internal static class ErrorHandler | ||
{ | ||
/// <summary> | ||
/// Method to handles errors while calculating | ||
/// </summary> | ||
/// <param name="icon">Path to result icon.</param> | ||
/// <param name="isGlobalQuery">Bool to indicate if it is a global query.</param> | ||
/// <param name="queryInput">User input as string including the action keyword.</param> | ||
/// <param name="errorMessage">Error message if applicable.</param> | ||
/// <param name="exception">Exception if applicable.</param> | ||
/// <returns>List of results to show. Either an error message or an empty list.</returns> | ||
/// <exception cref="ArgumentException">Thrown if <paramref name="errorMessage"/> and <paramref name="exception"/> are both filled with their default values.</exception> | ||
internal static List<Result> OnError(string icon, bool isGlobalQuery, string queryInput, string errorMessage, Exception exception = default) | ||
{ | ||
string userMessage; | ||
|
||
if (errorMessage != default) | ||
{ | ||
Log.Error($"Failed to calculate <{queryInput}>: {errorMessage}", typeof(Calculator.Main)); | ||
userMessage = errorMessage; | ||
} | ||
else if (exception != default) | ||
{ | ||
Log.Exception($"Exception when query for <{queryInput}>", exception, exception.GetType()); | ||
userMessage = exception.Message; | ||
} | ||
else | ||
{ | ||
throw new ArgumentException("The arguments error and exception have default values. One of them has to be filled with valid error data (error message/exception)!"); | ||
} | ||
|
||
return isGlobalQuery ? new List<Result>() : new List<Result> { CreateErrorResult(userMessage, icon) }; | ||
} | ||
|
||
private static Result CreateErrorResult(string errorMessage, string iconPath) | ||
{ | ||
return new Result | ||
{ | ||
Title = Properties.Resources.wox_plugin_calculator_calculation_failed, | ||
SubTitle = errorMessage, | ||
IcoPath = iconPath, | ||
Score = 300, | ||
}; | ||
} | ||
} | ||
} |
Oops, something went wrong.