[PTRun][Unit]Support for plural units and improve alternative spellings (#19961)

* Upgrade UnitsNet package to a version that supports plural

* Add support for plurals

Fix metre conversion
Add and update unit tests
This commit is contained in:
Floris Westerman
2022-08-24 11:50:34 +02:00
committed by GitHub
parent 13db8575e0
commit f44bf99dfd
8 changed files with 54 additions and 43 deletions

View File

@@ -43,6 +43,16 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
CollectionAssert.AreEqual(expectedResult, input); CollectionAssert.AreEqual(expectedResult, input);
} }
[DataTestMethod]
[DataRow(new string[] { "1", "metre", "in", "metre" }, new object[] { new string[] { "1", "meter", "in", "meter" } })]
[DataRow(new string[] { "1", "centimetre", "in", "kilometre" }, new object[] { new string[] { "1", "centimeter", "in", "kilometer" } })]
[DataRow(new string[] { "1", "metres", "in", "kilometres" }, new object[] { new string[] { "1", "meters", "in", "kilometers" } })]
public void HandlesMetreVsMeterNotation(string[] input, string[] expectedResult)
{
InputInterpreter.MetreToMeter(ref input);
CollectionAssert.AreEqual(expectedResult, input);
}
[DataTestMethod] [DataTestMethod]
[DataRow(new string[] { "5", "CeLsIuS", "in", "faHrenheiT" }, new object[] { new string[] { "5", "DegreeCelsius", "in", "DegreeFahrenheit" } })] [DataRow(new string[] { "5", "CeLsIuS", "in", "faHrenheiT" }, new object[] { new string[] { "5", "DegreeCelsius", "in", "DegreeFahrenheit" } })]
[DataRow(new string[] { "5", "f", "in", "celsius" }, new object[] { new string[] { "5", "<22>f", "in", "DegreeCelsius" } })] [DataRow(new string[] { "5", "f", "in", "celsius" }, new object[] { new string[] { "5", "<22>f", "in", "DegreeCelsius" } })]

View File

@@ -14,7 +14,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
public void HandleTemperature() public void HandleTemperature()
{ {
var convertModel = new ConvertModel(1, "DegreeCelsius", "DegreeFahrenheit"); var convertModel = new ConvertModel(1, "DegreeCelsius", "DegreeFahrenheit");
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.QuantityType.Temperature); double result = UnitHandler.ConvertInput(convertModel, UnitsNet.Temperature.Info);
Assert.AreEqual(33.79999999999999d, result); Assert.AreEqual(33.79999999999999d, result);
} }
@@ -22,7 +22,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
public void HandleLength() public void HandleLength()
{ {
var convertModel = new ConvertModel(1, "meter", "centimeter"); var convertModel = new ConvertModel(1, "meter", "centimeter");
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.QuantityType.Length); double result = UnitHandler.ConvertInput(convertModel, UnitsNet.Length.Info);
Assert.AreEqual(100, result); Assert.AreEqual(100, result);
} }
@@ -30,15 +30,23 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter.UnitTest
public void HandleNanometerToKilometer() public void HandleNanometerToKilometer()
{ {
var convertModel = new ConvertModel(1, "nanometer", "kilometer"); var convertModel = new ConvertModel(1, "nanometer", "kilometer");
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.QuantityType.Length); double result = UnitHandler.ConvertInput(convertModel, UnitsNet.Length.Info);
Assert.AreEqual(1E-12, result); Assert.AreEqual(1E-12, result);
} }
[TestMethod]
public void HandlePlurals()
{
var convertModel = new ConvertModel(1, "meters", "centimeters");
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.Length.Info);
Assert.AreEqual(100, result);
}
[TestMethod] [TestMethod]
public void HandlesByteCapitals() public void HandlesByteCapitals()
{ {
var convertModel = new ConvertModel(1, "kB", "kb"); var convertModel = new ConvertModel(1, "kB", "kb");
double result = UnitHandler.ConvertInput(convertModel, UnitsNet.QuantityType.Information); double result = UnitHandler.ConvertInput(convertModel, UnitsNet.Information.Info);
Assert.AreEqual(8, result); Assert.AreEqual(8, result);
} }

View File

@@ -44,7 +44,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="UnitsNet" Version="4.76.0" /> <PackageReference Include="UnitsNet" Version="4.144.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -169,19 +169,12 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
} }
/// <summary> /// <summary>
/// Converts spelling "metre" to "meter" /// Converts spelling "metre" to "meter", also for centimetre and other variants
/// </summary> /// </summary>
public static void MetreToMeter(ref string[] split) public static void MetreToMeter(ref string[] split)
{ {
if (split[1].ToLowerInvariant() == "metre") split[1] = split[1].Replace("metre", "meter", System.StringComparison.CurrentCultureIgnoreCase);
{ split[3] = split[3].Replace("metre", "meter", System.StringComparison.CurrentCultureIgnoreCase);
split[1] = "meter";
}
if (split[3].ToLowerInvariant() == "metre")
{
split[3] = "meter";
}
} }
/// <summary> /// <summary>

View File

@@ -65,7 +65,7 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
Title = result.ToString(), Title = result.ToString(),
IcoPath = _icon_path, IcoPath = _icon_path,
Score = 300, Score = 300,
SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.copy_to_clipboard, result.QuantityType), SubTitle = string.Format(CultureInfo.CurrentCulture, Properties.Resources.copy_to_clipboard, result.QuantityInfo.Name),
Action = c => Action = c =>
{ {
var ret = false; var ret = false;

View File

@@ -14,13 +14,13 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
public string UnitName { get; } public string UnitName { get; }
public QuantityType QuantityType { get; } public QuantityInfo QuantityInfo { get; }
public UnitConversionResult(double convertedValue, string unitName, QuantityType quantityType) public UnitConversionResult(double convertedValue, string unitName, QuantityInfo quantityInfo)
{ {
ConvertedValue = convertedValue; ConvertedValue = convertedValue;
UnitName = unitName; UnitName = unitName;
QuantityType = quantityType; QuantityInfo = quantityInfo;
} }
public string ToString(System.IFormatProvider provider = null) public string ToString(System.IFormatProvider provider = null)

View File

@@ -14,21 +14,21 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
{ {
private static readonly int _roundingFractionalDigits = 4; private static readonly int _roundingFractionalDigits = 4;
private static readonly QuantityType[] _included = new QuantityType[] private static readonly QuantityInfo[] _included = new QuantityInfo[]
{ {
QuantityType.Acceleration, Acceleration.Info,
QuantityType.Angle, Angle.Info,
QuantityType.Area, Area.Info,
QuantityType.Duration, Duration.Info,
QuantityType.Energy, Energy.Info,
QuantityType.Information, Information.Info,
QuantityType.Length, Length.Info,
QuantityType.Mass, Mass.Info,
QuantityType.Power, Power.Info,
QuantityType.Pressure, Pressure.Info,
QuantityType.Speed, Speed.Info,
QuantityType.Temperature, Temperature.Info,
QuantityType.Volume, Volume.Info,
}; };
/// <summary> /// <summary>
@@ -37,7 +37,9 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
/// <returns>Corresponding enum or null.</returns> /// <returns>Corresponding enum or null.</returns>
private static Enum GetUnitEnum(string unit, QuantityInfo unitInfo) private static Enum GetUnitEnum(string unit, QuantityInfo unitInfo)
{ {
UnitInfo first = Array.Find(unitInfo.UnitInfos, info => info.Name.ToLowerInvariant() == unit.ToLowerInvariant()); UnitInfo first = Array.Find(unitInfo.UnitInfos, info =>
unit.ToLowerInvariant() == info.Name.ToLowerInvariant() || unit.ToLowerInvariant() == info.PluralName.ToLowerInvariant());
if (first != null) if (first != null)
{ {
return first.Value; return first.Value;
@@ -72,12 +74,10 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
/// Given parsed ConvertModel, computes result. (E.g "1 foot in cm"). /// Given parsed ConvertModel, computes result. (E.g "1 foot in cm").
/// </summary> /// </summary>
/// <returns>The converted value as a double.</returns> /// <returns>The converted value as a double.</returns>
public static double ConvertInput(ConvertModel convertModel, QuantityType quantityType) public static double ConvertInput(ConvertModel convertModel, QuantityInfo quantityInfo)
{ {
QuantityInfo unitInfo = Quantity.GetInfo(quantityType); var fromUnit = GetUnitEnum(convertModel.FromUnit, quantityInfo);
var toUnit = GetUnitEnum(convertModel.ToUnit, quantityInfo);
var fromUnit = GetUnitEnum(convertModel.FromUnit, unitInfo);
var toUnit = GetUnitEnum(convertModel.ToUnit, unitInfo);
if (fromUnit != null && toUnit != null) if (fromUnit != null && toUnit != null)
{ {
@@ -94,13 +94,13 @@ namespace Community.PowerToys.Run.Plugin.UnitConverter
public static IEnumerable<UnitConversionResult> Convert(ConvertModel convertModel) public static IEnumerable<UnitConversionResult> Convert(ConvertModel convertModel)
{ {
var results = new List<UnitConversionResult>(); var results = new List<UnitConversionResult>();
foreach (QuantityType quantityType in _included) foreach (var quantityInfo in _included)
{ {
double convertedValue = UnitHandler.ConvertInput(convertModel, quantityType); double convertedValue = UnitHandler.ConvertInput(convertModel, quantityInfo);
if (!double.IsNaN(convertedValue)) if (!double.IsNaN(convertedValue))
{ {
UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityType); UnitConversionResult result = new UnitConversionResult(Round(convertedValue), convertModel.ToUnit, quantityInfo);
results.Add(result); results.Add(result);
} }
} }

View File

@@ -90,7 +90,7 @@
<PackageReference Include="Microsoft.VCRTForwarders.140" Version="1.0.7" /> <PackageReference Include="Microsoft.VCRTForwarders.140" Version="1.0.7" />
<PackageReference Include="System.Data.OleDb" Version="6.0.0" /> <PackageReference Include="System.Data.OleDb" Version="6.0.0" />
<PackageReference Include="System.ServiceProcess.ServiceController" Version="6.0.0" /> <PackageReference Include="System.ServiceProcess.ServiceController" Version="6.0.0" />
<PackageReference Include="UnitsNet" Version="4.76.0" /> <PackageReference Include="UnitsNet" Version="4.144.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>