Skip to content

Commit

Permalink
Change Parse to require a CultureInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Dec 5, 2023
1 parent d17e496 commit 91ab1ac
Show file tree
Hide file tree
Showing 20 changed files with 96 additions and 86 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 <contact@ppy.sh>. 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 <contact@ppy.sh>. 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
8 changes: 4 additions & 4 deletions osu.Framework.Tests/Bindables/BindableDoubleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,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 @@ -68,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 @@ -83,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 Down Expand Up @@ -127,7 +127,7 @@ public void TestParsingNumberLocale(double value, string locale, string expected
var bindable = new BindableDouble(value);
string? asString = bindable.ToString();
Assert.AreEqual(expected, asString);
Assert.DoesNotThrow(() => bindable.Parse(asString));
Assert.DoesNotThrow(() => bindable.Parse(asString, CultureInfo.CurrentCulture));
Assert.AreEqual(value, bindable.Value, Precision.DOUBLE_EPSILON);
}
}
Expand Down
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
8 changes: 4 additions & 4 deletions osu.Framework.Tests/Bindables/BindableFloatTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,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 @@ -68,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 @@ -83,7 +83,7 @@ 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);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public void TestParsingNumberLocale(float value, string locale, string expected)
var bindable = new BindableFloat(value);
string? asString = bindable.ToString();
Assert.AreEqual(expected, asString);
Assert.DoesNotThrow(() => bindable.Parse(asString));
Assert.DoesNotThrow(() => bindable.Parse(asString, CultureInfo.CurrentCulture));
Assert.AreEqual(value, bindable.Value, Precision.FLOAT_EPSILON);
}
}
Expand Down
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 <contact@ppy.sh>. 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
29 changes: 15 additions & 14 deletions osu.Framework.Tests/Bindables/BindableListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
Expand Down Expand Up @@ -135,7 +136,7 @@ public void TestBindCollectionChangedNotRunIfParsingSequenceEqualEnumerable()

NotifyCollectionChangedEventArgs triggeredArgs = null;
list.BindCollectionChanged((_, args) => triggeredArgs = args);
list.Parse(enumerable);
list.Parse(enumerable, CultureInfo.InvariantCulture);

Assert.That(triggeredArgs, Is.Null);
}
Expand Down Expand Up @@ -1488,7 +1489,7 @@ public void TestParseWithNullClearsList()
{
bindableStringList.Add("a item");

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

Assert.IsEmpty(bindableStringList);
}
Expand All @@ -1498,7 +1499,7 @@ public void TestParseWithArray()
{
IEnumerable<string> strings = new[] { "testA", "testB" };

bindableStringList.Parse(strings);
bindableStringList.Parse(strings, CultureInfo.InvariantCulture);

CollectionAssert.AreEquivalent(strings, bindableStringList);
}
Expand All @@ -1510,11 +1511,11 @@ public void TestParseWithDisabledListThrowsInvalidOperationException()

Assert.Multiple(() =>
{
Assert.Throws(typeof(InvalidOperationException), () => bindableStringList.Parse(null));
Assert.Throws(typeof(InvalidOperationException), () => bindableStringList.Parse(null, CultureInfo.InvariantCulture));
Assert.Throws(typeof(InvalidOperationException), () => bindableStringList.Parse(new object[]
{
"test", "testabc", "asdasdasdasd"
}));
}, CultureInfo.InvariantCulture));
});
}

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

Expand All @@ -1542,7 +1543,7 @@ public void TestParseWithNullNotifiesClearSubscribers()
var triggeredArgs = new List<NotifyCollectionChangedEventArgs>();
bindableStringList.CollectionChanged += (_, args) => triggeredArgs.Add(args);

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

Assert.That(triggeredArgs, Has.Count.EqualTo(1));
Assert.That(triggeredArgs.First().Action, Is.EqualTo(NotifyCollectionChangedAction.Remove));
Expand All @@ -1559,7 +1560,7 @@ public void TestParseWithItemsNotifiesAddRangeAndClearSubscribers()
var triggeredArgs = new List<NotifyCollectionChangedEventArgs>();
bindableStringList.CollectionChanged += (_, args) => triggeredArgs.Add(args);

bindableStringList.Parse(strings);
bindableStringList.Parse(strings, CultureInfo.InvariantCulture);

Assert.That(triggeredArgs, Has.Count.EqualTo(2));
Assert.That(triggeredArgs.First().Action, Is.EqualTo(NotifyCollectionChangedAction.Remove));
Expand Down
7 changes: 4 additions & 3 deletions osu.Framework.Tests/Bindables/BindableLongTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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(long value)
public void TestParsingString(string value, long expected)
{
var bindable = new BindableLong();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -43,7 +44,7 @@ public void TestParsingString(string value, long expected)
public void TestParsingStringWithRange(string value, long minValue, long maxValue, long expected)
{
var bindable = new BindableLong { 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, long minValue, long maxValu
public void TestParsingLong(long value)
{
var bindable = new BindableLong();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(value, bindable.Value);
}
Expand Down
5 changes: 3 additions & 2 deletions osu.Framework.Tests/Bindables/BindableMarginPaddingTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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 @@ -31,7 +32,7 @@ public void TestSet(float top, float left, float bottom, float right)
public void TestParsingString(string value, float expectedTop, float expectedLeft, float expectedBottom, float expectedRight)
{
var bindable = new BindableMarginPadding();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(new MarginPadding { Top = expectedTop, Left = expectedLeft, Bottom = expectedBottom, Right = expectedRight }, bindable.Value);
}
Expand All @@ -51,7 +52,7 @@ public void TestParsingStringWithRange(string value,
var expected = new MarginPadding { Top = expectedTop, Left = expectedLeft, Bottom = expectedBottom, Right = expectedRight };

var bindable = new BindableMarginPadding { MinValue = minValue, MaxValue = maxValue };
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

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

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

namespace osu.Framework.Tests.Bindables
Expand Down Expand Up @@ -32,7 +33,7 @@ public void TestSet(int width, int height)
public void TestParsingString(string value, int expectedWidth, int expectedHeight)
{
var bindable = new BindableSize();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(new Size(expectedWidth, expectedHeight), bindable.Value);
}
Expand All @@ -53,7 +54,7 @@ public void TestParsingStringWithRange(string value,
var expected = new Size(expectedWidth, expectedHeight);

var bindable = new BindableSize { MinValue = minValue, MaxValue = maxValue };
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, bindable.Value);
}
Expand All @@ -67,7 +68,7 @@ public void TestParsingSize(int width, int height)
var value = new Size(width, height);

var bindable = new BindableSize();
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/BindableStringTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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 @@ -24,7 +25,7 @@ public void TestSet(string value)
public void TestParsingString(string value)
{
var bindable = new Bindable<string>();
bindable.Parse(value);
bindable.Parse(value, CultureInfo.InvariantCulture);

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

0 comments on commit 91ab1ac

Please sign in to comment.