[PTRun][Calculator]Replace input with result with '=' key (#31391)

* replace input with result

* fix and add test

* add options and use result property
This commit is contained in:
Davide Giacometti
2024-02-20 13:08:54 +01:00
committed by GitHub
parent e573b7a1b1
commit 92c85630a9
8 changed files with 121 additions and 37 deletions

View File

@@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using Microsoft.PowerToys.Settings.UI.Library;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Wox.Plugin;
@@ -13,6 +14,19 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
[TestClass]
public class QueryTests
{
private static Mock<Main> _main;
private static Mock<IPublicAPI> _api;
[TestInitialize]
public void Initialize()
{
_api = new Mock<IPublicAPI>();
_api.Setup(api => api.ChangeQuery(It.IsAny<string>(), It.IsAny<bool>())).Verifiable();
_main = new Mock<Main>();
_main.Object.Init(new PluginInitContext() { API = _api.Object });
}
[DataTestMethod]
[DataRow("=pi(9+)", "Expression wrong or incomplete (Did you forget some parentheses?)")]
[DataRow("=pi,", "Expression wrong or incomplete (Did you forget some parentheses?)")]
@@ -28,12 +42,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
[DataRow("10+(8*9)/0*7", "Expression contains division by zero")]
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;
var result = _main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
// Assert
Assert.AreEqual(expectedResult, result);
@@ -55,11 +67,10 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void NoResultOnInvalidGlobalQuery(string typedString)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
// Act
var result = main.Object.Query(expectedQuery).Count;
var result = _main.Object.Query(expectedQuery).Count;
// Assert
Assert.AreEqual(result, 0);
@@ -79,13 +90,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void NoResultIfQueryEndsWithBinaryOperator(string typedString)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).Count;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).Count;
var result = _main.Object.Query(expectedQuery).Count;
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).Count;
// Assert
Assert.AreEqual(result, 0);
@@ -100,13 +110,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void NoErrorForDivisionByNumberWithDecimalDigits(string typedString)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault().SubTitle;
var result = _main.Object.Query(expectedQuery).FirstOrDefault().SubTitle;
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault().SubTitle;
// Assert
Assert.AreEqual(result, "Copy this number to the clipboard");
@@ -190,13 +199,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void NoErrorForHumanMultiplicationExpressions(string typedString)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.SubTitle;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.SubTitle;
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.SubTitle;
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.SubTitle;
// Assert
Assert.AreEqual("Copy this number to the clipboard", result);
@@ -221,13 +229,12 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void RightAnswerForHumanMultiplicationExpressions(string typedString, double answer)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
// Assert
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
@@ -242,17 +249,34 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void RightAnswerForLargeHexadecimalNumbers(string typedString, double answer)
{
// Setup
Mock<Main> main = new();
Query expectedQuery = new(typedString);
Query expectedQueryWithKeyword = new("=" + typedString, "=");
// Act
var result = main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
var resultWithKeyword = main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
var result = _main.Object.Query(expectedQuery).FirstOrDefault()?.Title;
var resultWithKeyword = _main.Object.Query(expectedQueryWithKeyword).FirstOrDefault()?.Title;
// Assert
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), result);
Assert.AreEqual(answer.ToString(CultureInfo.CurrentCulture), resultWithKeyword);
}
[DataTestMethod]
[DataRow("#", "#1+1=", true)]
[DataRow("#", "#1+1", false)]
[DataRow("#", "#1/0=", false)]
[DataRow("", "1+1=", false)]
public void HandleInputReplace(string actionKeyword, string query, bool shouldChangeQuery)
{
// Setup
Query expectedQuery = new(query, actionKeyword);
_main.Object.UpdateSettings(new PowerLauncherPluginSettings()); // Default settings
// Act
_main.Object.Query(expectedQuery);
// Assert
_api.Verify(api => api.ChangeQuery(It.IsAny<string>(), It.IsAny<bool>()), shouldChangeQuery ? Times.Once : Times.Never);
}
}
}