mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
## Summary of the Pull Request This PR continues the tradition of alphabetical progress. After [MBGA](#41961), we move on to **MCBA — Make Calculator Better Again!** - Introduces limited automatic correction and completion of expressions. - The goal is to allow uninterrupted typing and avoid disruptions when a partially entered expression is temporarily invalid (which previously caused the result to be replaced by an error message or hidden by the fallback). - The implementation intentionally aims for a sweet spot: - Ignores trailing binary operators. - Automatically closes all opened parentheses. - It is not exhaustive; for example, incomplete constants or functions may still result in an invalid query. - Copy current result to the search bar. - Adds an option to copy the current result to the search bar when the user types `=` at the end of the expression. - Adds a new menu item for the same action. - Fixes the **Save** command to also copy the result to the query. - Adds support for the `factorial(x)` function and the `x!` expression. - Factorial calculations are supported up to `170!` (limited by `double`), but display is constrained by decimal conversion and allows direct display of results up to `20!`. - Adds support for the `sign(x)` function. - Adds support for the `π` symbol as an alternative to the `pi` constant. - Adds a context menu item to the result list item and fallback that displays the octal representation of the result. - Implements beautification of the query: - Converts technical symbols such as `*` or `/` to `×` or `÷`, respectively. - Not enabled for fallbacks for now, since the item text should match the query to keep the score intact. - Implements additional normalization of symbols in the query: - Percent: `%`, `%`, `﹪` - Minus: `−`, `-`, `–`, `—` - Factorial: `!`, `!` - Multiplication: `*`, `×`, `∗`, `·`, `⋅`, `✕`, `✖`, `\u2062` (invisible times) - Division: `/`, `÷`, `➗`, `:` - Allows use of `²` and `³` as alternatives to `^2` and `^3`. - Updates the unit test that was culture sensitive to force en-US output (not an actual fix, but at least it clears false positive for now) - Fixes pre-parsing of scientific notation to prevent capturing minus sign as part of it. - Fixes normalization/rounding of the result, so it can display small values (the current solution turned it into a string with scientific notation and couldn't parse it back). - Updates test with new cases ## Pictures? Moving! Previous behavior: https://github.com/user-attachments/assets/ebcdcd85-797a-44f9-a8b1-a0f2f33c6b42 New behavior: https://github.com/user-attachments/assets/5bd94663-a0d0-4d7d-8032-1030e79926c3 <!-- Please review the items on the PR checklist before submitting--> ## PR Checklist - [x] Closes: #43481 - [x] Closes: #43460 - [x] Closes: #42078 - [x] Closes: #41839 - [x] Closes: #39659 - [x] Closes: #40502 - [x] Related to: #41715 <!-- - [ ] Closes: #yyy (add separate lines for additional resolved issues) --> - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [ ] **Localization:** All end-user-facing strings can be localized - [ ] **Dev docs:** Added/updated - [ ] **New binaries:** Added on the required places - [ ] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [ ] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [ ] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [ ] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [ ] **Documentation updated:** If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys) and link it here: #xxx <!-- Provide a more detailed description of the PR, other things fixed, or any additional comments/features here --> ## Detailed Description of the Pull Request / Additional comments <!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well --> ## Validation Steps Performed
86 lines
2.8 KiB
C#
86 lines
2.8 KiB
C#
// 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.CmdPal.Ext.Calc.Helper;
|
|
using Microsoft.CmdPal.Ext.Calc.Pages;
|
|
using Microsoft.CmdPal.Ext.UnitTestBase;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace Microsoft.CmdPal.Ext.Calc.UnitTests;
|
|
|
|
[TestClass]
|
|
public class QueryTests : CommandPaletteUnitTestBase
|
|
{
|
|
[DataTestMethod]
|
|
[DataRow("2+2", "4")]
|
|
[DataRow("5*3", "15")]
|
|
[DataRow("10/2", "5")]
|
|
[DataRow("sqrt(16)", "4")]
|
|
[DataRow("2^3", "8")]
|
|
public void TopLevelPageQueryTest(string input, string expectedResult)
|
|
{
|
|
var settings = new Settings();
|
|
var page = new CalculatorListPage(settings);
|
|
|
|
// Simulate query execution
|
|
page.UpdateSearchText(string.Empty, input);
|
|
var result = page.GetItems();
|
|
|
|
Assert.IsTrue(result.Length == 1, "Valid input should always return result");
|
|
|
|
var firstResult = result.FirstOrDefault();
|
|
|
|
Assert.IsNotNull(result);
|
|
Assert.IsTrue(
|
|
firstResult.Title.Contains(expectedResult),
|
|
$"Expected result to contain '{expectedResult}' but got '{firstResult.Title}'");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void EmptyQueryTest()
|
|
{
|
|
var settings = new Settings();
|
|
var page = new CalculatorListPage(settings);
|
|
page.UpdateSearchText("abc", string.Empty);
|
|
var results = page.GetItems();
|
|
Assert.IsNotNull(results);
|
|
|
|
var firstItem = results.FirstOrDefault();
|
|
Assert.AreEqual("Type an equation...", firstItem.Title);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void InvalidExpressionTest()
|
|
{
|
|
var settings = new Settings();
|
|
|
|
var page = new CalculatorListPage(settings);
|
|
|
|
// Simulate query execution
|
|
page.UpdateSearchText(string.Empty, "invalid expression");
|
|
var result = page.GetItems().FirstOrDefault();
|
|
|
|
Assert.AreEqual("Type an equation...", result.Title);
|
|
}
|
|
|
|
[DataTestMethod]
|
|
[DataRow("sin(60)", "-0.30481", CalculateEngine.TrigMode.Radians)]
|
|
[DataRow("sin(60)", "0.866025", CalculateEngine.TrigMode.Degrees)]
|
|
[DataRow("sin(60)", "0.809016", CalculateEngine.TrigMode.Gradians)]
|
|
public void TrigModeSettingsTest(string input, string expected, CalculateEngine.TrigMode trigMode)
|
|
{
|
|
var settings = new Settings(trigUnit: trigMode, outputUseEnglishFormat: true);
|
|
|
|
var page = new CalculatorListPage(settings);
|
|
|
|
page.UpdateSearchText(string.Empty, input);
|
|
var result = page.GetItems().FirstOrDefault();
|
|
|
|
Assert.IsNotNull(result);
|
|
|
|
Assert.IsTrue(result.Title.Contains(expected, System.StringComparison.Ordinal), $"Calc trigMode convert result isn't correct. Current result: {result.Title}");
|
|
}
|
|
}
|