Skip to content

Commit

Permalink
feat(DecimalFormatter): Implement FormatDouble and ParseDouble
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadHadi2031 committed Mar 8, 2022
1 parent c3ff19c commit 3e103af
Show file tree
Hide file tree
Showing 23 changed files with 703 additions and 315 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ public void UpDownTest()
Assert.AreEqual(55, numBox.GetDependencyPropertyValue<double>("Value"));
}

[Test]
[AutoRetry]
public void DecimalFormatterTest()
{
Run("UITests.Shared.Microsoft_UI_Xaml_Controls.NumberBoxTests.MUX_Test");

var numBox = _app.Marked("TestNumberBox");
Assert.AreEqual(0, numBox.GetDependencyPropertyValue<double>("Value"));

_app.FastTap("MinCheckBox");
_app.FastTap("MaxCheckBox");
_app.Marked("CustomFormatterButton").FastTap();

numBox.ClearText();
numBox.EnterText("۱٫۷");
_app.PressEnter();

Assert.AreEqual("۱٫۷۵", numBox.GetDependencyPropertyValue<string>("Text"));
Assert.AreEqual(1.75, numBox.GetDependencyPropertyValue<double>("Value"));
}

[Test]
[AutoRetry]
public void MinMaxTest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ private void NumberBoxValueChanged(object sender, Microsoft.UI.Xaml.Controls.Num

private void CustomFormatterButton_Click(object sender, RoutedEventArgs e)
{
List<string> languages = new List<string>() { "fr-FR" };
DecimalFormatter formatter = new DecimalFormatter(languages, "FR");
DecimalFormatter formatter = new DecimalFormatter();
formatter.IntegerDigits = 1;
formatter.FractionDigits = 2;
formatter.NumeralSystem = "ArabExt";
formatter.NumberRounder = new IncrementNumberRounder { Increment = 0.25 };

TestNumberBox.NumberFormatter = formatter;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI.RuntimeTests/UnitTestsImport.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
<Compile Include="$(MSBuildThisFileDirectory)..\Uno.UI.Tests\Windows_Globalization\Given_NumeralSystemTranslator.cs">
<Link>UnitTests\Windows_Globalization\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)..\Uno.UI.Tests\Windows_Globalization\Given_DecimalFormatter.cs">
<Link>UnitTests\Windows_Globalization\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)..\Uno.UI.Tests\Windows_Globalization\Given_PercentFormatter.cs">
<Link>UnitTests\Windows_Globalization\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)..\Uno.UI.Tests\Windows_Globalization\Given_PermilleFormatter.cs">
<Link>UnitTests\Windows_Globalization\%(RecursiveDir)%(FileName)%(Extension)</Link>
</Compile>
</ItemGroup>


Expand Down
290 changes: 290 additions & 0 deletions src/Uno.UI.Tests/Windows_Globalization/Given_DecimalFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Windows.Globalization.NumberFormatting;

namespace Uno.UI.Tests.Windows_Globalization
{
[TestClass]
public class Given_DecimalFormatter
{
[DataTestMethod]
[DataRow(double.PositiveInfinity, "∞")]
[DataRow(double.NegativeInfinity, "-∞")]
[DataRow(double.NaN, "NaN")]
public void When_FormatSpecialDouble(double value, string expected)
{
DecimalFormatter df = new DecimalFormatter();
var actual = df.FormatDouble(value);

Assert.AreEqual(expected, actual);
}

[DataTestMethod]
[DataRow(1.5d, 1, 2, "1.50")]
[DataRow(1.567d, 1, 2, "1.567")]
[DataRow(1.5602d, 1, 2, "1.5602")]
[DataRow(0d, 0, 0, "0")]
[DataRow(-0d, 0, 0, "0")]
[DataRow(0d, 0, 2, ".00")]
[DataRow(-0d, 0, 2, ".00")]
[DataRow(0d, 2, 0, "00")]
[DataRow(-0d, 2, 0, "00")]
[DataRow(0d, 3, 1, "000.0")]
[DataRow(-0d, 3, 1, "000.0")]
public void When_FormatDouble(double value, int integerDigits, int fractionDigits, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.IntegerDigits = integerDigits;
df.FractionDigits = fractionDigits;

var formatted = df.FormatDouble(value);
Assert.AreEqual(expected, formatted);
}

[DataTestMethod]
[DataRow(1234, 2, 0, "1,234")]
[DataRow(1234, 6, 0, "001,234")]
[DataRow(1234.56, 2, 2, "1,234.56")]
[DataRow(1234.0, 6, 2, "001,234.00")]
[DataRow(1234.0, 6, 0, "001,234")]
public void When_FormatDoubleWithIsGroupSetTrue(double value, int integerDigits, int fractionDigits, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.IntegerDigits = integerDigits;
df.FractionDigits = fractionDigits;
df.IsGrouped = true;

var formatted = df.FormatDouble(value);
Assert.AreEqual(expected, formatted);
}

[DataTestMethod]
[DataRow(0, 0, "-0")]
[DataRow(0, 2, "-.00")]
[DataRow(2, 0, "-00")]
[DataRow(3, 1, "-000.0")]
public void When_FormatDoubleMinusZeroWithIsZeroSignedSetTrue(int integerDigits, int fractionDigits, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.IntegerDigits = integerDigits;
df.FractionDigits = fractionDigits;
df.IsZeroSigned = true;

var formatted = df.FormatDouble(-0d);
Assert.AreEqual(expected, formatted);
}

[DataTestMethod]
[DataRow(0, 0, "0")]
[DataRow(0, 2, ".00")]
[DataRow(2, 0, "00")]
[DataRow(3, 1, "000.0")]
public void When_FormatDoubleZeroWithIsZeroSignedSetTrue(int integerDigits, int fractionDigits, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.IntegerDigits = integerDigits;
df.FractionDigits = fractionDigits;
df.IsZeroSigned = true;

var formatted = df.FormatDouble(0d);
Assert.AreEqual(expected, formatted);
}

[DataTestMethod]
[DataRow(1d, "1.")]
public void When_FormatDoubleWithIsDecimalPointerAlwaysDisplayedSetTrue(double value, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.IsDecimalPointAlwaysDisplayed = true;
df.FractionDigits = 0;
df.IntegerDigits = 0;

var formatted = df.FormatDouble(value);
Assert.AreEqual(expected, formatted);
}

[DataTestMethod]
[DataRow(123.4567d, 5, 1, 2, "123.4567")]
[DataRow(123.4567d, 10, 1, 2, "123.4567000")]
[DataRow(123.4567d, 2, 1, 2, "123.4567")]
[DataRow(12.3d, 4, 1, 2, "12.30")]
[DataRow(12.3d, 4, 1, 0, "12.30")]
public void When_FormatDoubleWithSpecificSignificantDigits(double value, int significantDigits, int integerDigits, int fractionDigits, string expected)
{
DecimalFormatter df = new DecimalFormatter();
df.SignificantDigits = significantDigits;
df.IntegerDigits = integerDigits;
df.FractionDigits = fractionDigits;

var formatted = df.FormatDouble(value);
Assert.AreEqual(expected, formatted);
}

[TestMethod]
public void When_FormatDoubleUsingIncrementNumberRounder()
{
DecimalFormatter df = new DecimalFormatter();
IncrementNumberRounder rounder = new IncrementNumberRounder();
rounder.Increment = 0.5;
df.NumberRounder = rounder;
var formatted = df.FormatDouble(1.8);

Assert.AreEqual("2.00", formatted);
}

[TestMethod]
public void When_FormatDoubleUsingSignificantDigitsNumberRounder()
{
DecimalFormatter df = new DecimalFormatter();
SignificantDigitsNumberRounder rounder = new SignificantDigitsNumberRounder();
rounder.SignificantDigits = 1;
df.NumberRounder = rounder;
var formatted = df.FormatDouble(1.8);

Assert.AreEqual("2.00", formatted);
}

[TestMethod]
public void When_Initialize()
{
DecimalFormatter df = new DecimalFormatter();

Assert.AreEqual(0, df.SignificantDigits);
Assert.AreEqual(1, df.IntegerDigits);
Assert.AreEqual(2, df.FractionDigits);
Assert.AreEqual(false, df.IsGrouped);
Assert.AreEqual(false, df.IsZeroSigned);
Assert.AreEqual(false, df.IsDecimalPointAlwaysDisplayed);
Assert.AreEqual("en-US", df.ResolvedLanguage);
Assert.IsNull(df.NumberRounder);
/*
FractionDigits 2 int
GeographicRegion "US" string
IntegerDigits 1 int
IsDecimalPointAlwaysDisplayed false bool
IsGrouped false bool
IsZeroSigned false bool
NumberRounder null WindoGlobalization.NumberFormatting.INumberRounder
NumeralSystem "Latn" string
ResolvedGeographicRegion "ZZ" string
ResolvedLanguage "en-US" string
SignificantDigits 0 int
*/
}

[DataTestMethod]
[DataRow("1.2", 1.2)]
[DataRow("1.20", 1.2)]
[DataRow("12,34.2", null)]
[DataRow("0", 0d)]
public void When_ParseDouble(string value, double? expected)
{
DecimalFormatter df = new DecimalFormatter();
df.FractionDigits = 2;

var actual = df.ParseDouble(value);
Assert.AreEqual(expected, actual);
}

[DataTestMethod]
[DataRow("1234.2", 1234.2)]
[DataRow("1,234.2", 1234.2)]
[DataRow("12,34.2", null)]
public void When_ParseDoubleAndIsGroupSetTrue(string value, double? expected)
{
DecimalFormatter df = new DecimalFormatter();
df.FractionDigits = 2;
df.IsGrouped = true;

var actual = df.ParseDouble(value);
Assert.AreEqual(expected, actual);
}

[DataTestMethod]
[DataRow("1", 1d)]
[DataRow("1.", 1d)]
public void When_ParseDoubleAndIsDecimalPointAlwaysDisplayedSetTrue(string value, double? expected)
{
DecimalFormatter df = new DecimalFormatter();
df.FractionDigits = 2;
df.IsDecimalPointAlwaysDisplayed = true;

var actual = df.ParseDouble(value);
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void When_ParseDoubleMinusZero()
{
DecimalFormatter df = new DecimalFormatter();
var actual = df.ParseDouble("-0");
bool isNegative = false;

if (actual.HasValue)
{
isNegative = BitConverter.DoubleToInt64Bits(actual.Value) < 0;
}

Assert.AreEqual(true, isNegative);
}

[DataTestMethod]
[DataRow("Arab")]
[DataRow("ArabExt")]
[DataRow("Bali")]
[DataRow("Beng")]
[DataRow("Cham")]
[DataRow("Deva")]
[DataRow("FullWide")]
[DataRow("Gujr")]
[DataRow("Guru")]
[DataRow("Java")]
[DataRow("Kali")]
[DataRow("Khmr")]
[DataRow("Knda")]
[DataRow("Lana")]
[DataRow("LanaTham")]
[DataRow("Laoo")]
[DataRow("Latn")]
[DataRow("Lepc")]
[DataRow("Limb")]
[DataRow("Mlym")]
[DataRow("Mong")]
[DataRow("Mtei")]
[DataRow("Mymr")]
[DataRow("MymrShan")]
[DataRow("Nkoo")]
[DataRow("Olck")]
[DataRow("Orya")]
[DataRow("Saur")]
[DataRow("Sund")]
[DataRow("Talu")]
[DataRow("TamlDec")]
[DataRow("Telu")]
[DataRow("Thai")]
[DataRow("Tibt")]
[DataRow("Vaii")]
public void When_ParseDoubleUsingSpeceficNumeralSystem(string numeralSystem)
{
DecimalFormatter df = new DecimalFormatter();
df.NumeralSystem = numeralSystem;

var translator = new NumeralSystemTranslator { NumeralSystem = numeralSystem };
var translated = translator.TranslateNumerals("1234.56789");

var actual = df.ParseDouble(translated);
Assert.AreEqual(1234.56789, actual);
}

[TestMethod]
public void When_ParseNotValidDouble()
{
DecimalFormatter df = new DecimalFormatter();

var actual = df.ParseDouble("a12");
Assert.AreEqual(null, actual);
}
}
}
Loading

0 comments on commit 3e103af

Please sign in to comment.