Skip to content

Commit

Permalink
Merge pull request #6065 from Neuheit/feature/5176
Browse files Browse the repository at this point in the history
Add format provider argument to `IParseable.Parse()`
  • Loading branch information
bdach authored Dec 5, 2023
2 parents 70fef99 + f71fbfb commit 5b64428
Show file tree
Hide file tree
Showing 22 changed files with 188 additions and 106 deletions.
5 changes: 3 additions & 2 deletions osu.Framework.Tests/Bindables/BindableBoolTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Bindables;

Expand All @@ -26,7 +27,7 @@ public void TestSet(bool value)
public void TestParsingString(string value, bool expected)
{
var bindable = new BindableBool();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -36,7 +37,7 @@ public void TestParsingString(string value, bool expected)
public void TestParsingBoolean(bool value)
{
var bindable = new BindableBool();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(value, bindable.Value);
}
Expand Down
3 changes: 2 additions & 1 deletion osu.Framework.Tests/Bindables/BindableColour4Test.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
Expand Down Expand Up @@ -41,7 +42,7 @@ public void TestSet(byte r, byte g, byte b, byte a)
public void TestParsingString(string value, Colour4 expected)
{
var bindable = new BindableColour4();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand Down
29 changes: 15 additions & 14 deletions osu.Framework.Tests/Bindables/BindableDictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -166,7 +167,7 @@ public void TestBindCollectionChangedNotRunIfParsingSequenceEqualEnumerable()

NotifyDictionaryChangedEventArgs<string, byte> triggeredArgs = null;
dict.BindCollectionChanged((_, args) => triggeredArgs = args);
dict.Parse(enumerable);
dict.Parse(enumerable, CultureInfo.InvariantCulture);

Assert.That(triggeredArgs, Is.Null);
}
Expand Down Expand Up @@ -900,7 +901,7 @@ public void TestParseWithNullClearsDictionary()
{
bindableStringByteDictionary.Add("a item", 0);

bindableStringByteDictionary.Parse(null);
bindableStringByteDictionary.Parse(null, CultureInfo.InvariantCulture);

Assert.IsEmpty(bindableStringByteDictionary);
}
Expand All @@ -914,7 +915,7 @@ public void TestParseWithArray()
new KeyValuePair<string, byte>("testB", 1),
};

bindableStringByteDictionary.Parse(array);
bindableStringByteDictionary.Parse(array, CultureInfo.InvariantCulture);

CollectionAssert.AreEquivalent(array, bindableStringByteDictionary);
}
Expand All @@ -926,13 +927,13 @@ public void TestParseWithDisabledDictionaryThrowsInvalidOperationException()

Assert.Multiple(() =>
{
Assert.Throws(typeof(InvalidOperationException), () => bindableStringByteDictionary.Parse(null));
Assert.Throws(typeof(InvalidOperationException), () => bindableStringByteDictionary.Parse(null, CultureInfo.InvariantCulture));
Assert.Throws(typeof(InvalidOperationException), () => bindableStringByteDictionary.Parse(new[]
{
new KeyValuePair<string, byte>("test", 0),
new KeyValuePair<string, byte>("testabc", 1),
new KeyValuePair<string, byte>("asdasdasdasd", 1),
}));
}, CultureInfo.InvariantCulture));
});
}

Expand All @@ -941,13 +942,13 @@ public void TestParseWithInvalidArgumentTypesThrowsArgumentException()
{
Assert.Multiple(() =>
{
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(""));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(new object()));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1.1));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1.1f));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse("test123"));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(29387L));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1, CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse("", CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(new object(), CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1.1, CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(1.1f, CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse("test123", CultureInfo.InvariantCulture));
Assert.Throws(typeof(ArgumentException), () => bindableStringByteDictionary.Parse(29387L, CultureInfo.InvariantCulture));
});
}

Expand All @@ -967,7 +968,7 @@ public void TestParseWithNullNotifiesClearSubscribers()
var triggeredArgs = new List<NotifyDictionaryChangedEventArgs<string, byte>>();
bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs.Add(args);

bindableStringByteDictionary.Parse(null);
bindableStringByteDictionary.Parse(null, CultureInfo.InvariantCulture);

Assert.That(triggeredArgs, Has.Count.EqualTo(1));
Assert.That(triggeredArgs.First().Action, Is.EqualTo(NotifyDictionaryChangedAction.Remove));
Expand All @@ -988,7 +989,7 @@ public void TestParseWithItemsNotifiesAddRangeAndClearSubscribers()
var triggeredArgs = new List<NotifyDictionaryChangedEventArgs<string, byte>>();
bindableStringByteDictionary.CollectionChanged += (_, args) => triggeredArgs.Add(args);

bindableStringByteDictionary.Parse(array);
bindableStringByteDictionary.Parse(array, CultureInfo.InvariantCulture);

Assert.That(triggeredArgs, Has.Count.EqualTo(2));
Assert.That(triggeredArgs.First().Action, Is.EqualTo(NotifyDictionaryChangedAction.Remove));
Expand Down
38 changes: 35 additions & 3 deletions osu.Framework.Tests/Bindables/BindableDoubleTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Utils;

namespace osu.Framework.Tests.Bindables
{
Expand Down Expand Up @@ -53,7 +55,7 @@ public void TestDefaultCheck(double value, double def, double? precision = null)
public void TestParsingString(string value, double expected)
{
var bindable = new BindableDouble();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -66,7 +68,7 @@ public void TestParsingString(string value, double expected)
public void TestParsingStringWithRange(string value, double minValue, double maxValue, double expected)
{
var bindable = new BindableDouble { MinValue = minValue, MaxValue = maxValue };
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -81,7 +83,7 @@ public void TestParsingStringWithRange(string value, double minValue, double max
public void TestParsingDouble(double value)
{
var bindable = new BindableDouble();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(value, bindable.Value);
}
Expand All @@ -98,5 +100,35 @@ public void TestPropagationToPlainBindable()
number.MinValue = 0;
number.MaxValue = 10;
}

[TestCase("1.4", "en-US", 1.4)]
[TestCase("1,4", "de-DE", 1.4)]
[TestCase("1.400,01", "de-DE", 1400.01)]
[TestCase("1 234,57", "ru-RU", 1234.57)]
[TestCase("1,094", "fr-FR", 1.094)]
[TestCase("1,400.01", "zh-CN", 1400.01)]
public void TestParsingStringLocale(string value, string locale, double expected)
{
var bindable = new BindableDouble();
bindable.Parse(value, CultureInfo.GetCultureInfo(locale));
Assert.AreEqual(expected, bindable.Value);
}

[TestCase(1.4, "en-US", "1.4")]
[TestCase(1.4, "de-DE", "1,4")]
[TestCase(1400.01, "de-DE", "1400,01")]
[TestCase(1234.57, "ru-RU", "1234,57")]
[TestCase(1.094, "fr-FR", "1,094")]
[TestCase(1400.01, "zh-CN", "1400.01")]
public void TestParsingNumberLocale(double value, string locale, string expected)
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(locale);

var bindable = new BindableDouble(value);
string? asString = bindable.ToString();
Assert.AreEqual(expected, asString);
Assert.DoesNotThrow(() => bindable.Parse(asString, CultureInfo.CurrentCulture));
Assert.AreEqual(value, bindable.Value, Precision.DOUBLE_EPSILON);
}
}
}
13 changes: 7 additions & 6 deletions osu.Framework.Tests/Bindables/BindableEnumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Globalization;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -32,8 +33,8 @@ public void TestParsing(TestEnum expected, params object[] values)

foreach (object value in values.Append(expected))
{
bindable.Parse(value);
nullable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);
nullable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
Assert.AreEqual(expected, nullable.Value);
Expand All @@ -47,8 +48,8 @@ public void TestUnparsaebles(object value)
var bindable = new Bindable<TestEnum>();
var nullable = new Bindable<TestEnum?>();

Assert.Throws<ArgumentException>(() => bindable.Parse(value));
Assert.Throws<ArgumentException>(() => nullable.Parse(value));
Assert.Throws<ArgumentException>(() => bindable.Parse(value, CultureInfo.InvariantCulture));
Assert.Throws<ArgumentException>(() => nullable.Parse(value, CultureInfo.InvariantCulture));
}

[Test]
Expand All @@ -57,8 +58,8 @@ public void TestEmptyString()
var bindable = new Bindable<TestEnum>();
var nullable = new Bindable<TestEnum?>();

Assert.Throws<ArgumentException>(() => bindable.Parse(string.Empty));
nullable.Parse(string.Empty);
Assert.Throws<ArgumentException>(() => bindable.Parse(string.Empty, CultureInfo.InvariantCulture));
nullable.Parse(string.Empty, CultureInfo.InvariantCulture);

Assert.That(nullable.Value, Is.Null);
}
Expand Down
38 changes: 35 additions & 3 deletions osu.Framework.Tests/Bindables/BindableFloatTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Utils;

namespace osu.Framework.Tests.Bindables
{
Expand Down Expand Up @@ -53,7 +55,7 @@ public void TestDefaultCheck(float value, float def, float? precision = null)
public void TestParsingString(string value, float expected)
{
var bindable = new BindableFloat();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -66,7 +68,7 @@ public void TestParsingString(string value, float expected)
public void TestParsingStringWithRange(string value, float minValue, float maxValue, float expected)
{
var bindable = new BindableFloat { MinValue = minValue, MaxValue = maxValue };
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -81,9 +83,39 @@ public void TestParsingStringWithRange(string value, float minValue, float maxVa
public void TestParsingFloat(float value)
{
var bindable = new BindableFloat();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(value, bindable.Value);
}

[TestCase("1.4", "en-US", 1.4f)]
[TestCase("1,4", "de-DE", 1.4f)]
[TestCase("1.400,01", "de-DE", 1400.01f)]
[TestCase("1 234,57", "ru-RU", 1234.57f)]
[TestCase("1,094", "fr-FR", 1.094f)]
[TestCase("1,400.01", "zh-CN", 1400.01f)]
public void TestParsingStringLocale(string value, string locale, float expected)
{
var bindable = new BindableFloat();
bindable.Parse(value, CultureInfo.GetCultureInfo(locale));
Assert.AreEqual(expected, bindable.Value);
}

[TestCase(1.4f, "en-US", "1.4")]
[TestCase(1.4f, "de-DE", "1,4")]
[TestCase(1400.01f, "de-DE", "1400,01")]
[TestCase(1234.57f, "ru-RU", "1234,57")]
[TestCase(1.094f, "fr-FR", "1,094")]
[TestCase(1400.01f, "zh-CN", "1400.01")]
public void TestParsingNumberLocale(float value, string locale, string expected)
{
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(locale);

var bindable = new BindableFloat(value);
string? asString = bindable.ToString();
Assert.AreEqual(expected, asString);
Assert.DoesNotThrow(() => bindable.Parse(asString, CultureInfo.CurrentCulture));
Assert.AreEqual(value, bindable.Value, Precision.FLOAT_EPSILON);
}
}
}
7 changes: 4 additions & 3 deletions osu.Framework.Tests/Bindables/BindableIntTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Bindables;

Expand Down Expand Up @@ -30,7 +31,7 @@ public void TestSet(int value)
public void TestParsingString(string value, int expected)
{
var bindable = new BindableInt();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -43,7 +44,7 @@ public void TestParsingString(string value, int expected)
public void TestParsingStringWithRange(string value, int minValue, int maxValue, int expected)
{
var bindable = new BindableInt { MinValue = minValue, MaxValue = maxValue };
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -58,7 +59,7 @@ public void TestParsingStringWithRange(string value, int minValue, int maxValue,
public void TestParsingInt(int value)
{
var bindable = new BindableInt();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(value, bindable.Value);
}
Expand Down
Loading

0 comments on commit 5b64428

Please sign in to comment.