Commit d33bff04 authored by Jon Skeet's avatar Jon Skeet

Fixed i18n issues with numbers

parent 7a66a6d3
...@@ -228,8 +228,10 @@ namespace Google.ProtocolBuffers { ...@@ -228,8 +228,10 @@ namespace Google.ProtocolBuffers {
[Test] [Test]
public void ReflectionDefaults() { public void ReflectionDefaults() {
reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance); TestUtil.TestInMultipleCultures(() => {
reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build()); reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
});
} }
// ================================================================= // =================================================================
// Extensions. // Extensions.
...@@ -296,8 +298,10 @@ namespace Google.ProtocolBuffers { ...@@ -296,8 +298,10 @@ namespace Google.ProtocolBuffers {
[Test] [Test]
public void ExtensionReflectionDefaults() { public void ExtensionReflectionDefaults() {
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance); TestUtil.TestInMultipleCultures(() => {
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build()); extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
});
} }
[Test] [Test]
......
...@@ -31,10 +31,12 @@ ...@@ -31,10 +31,12 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
using Google.ProtocolBuffers.TestProtos; using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework; using NUnit.Framework;
using System.Threading;
namespace Google.ProtocolBuffers { namespace Google.ProtocolBuffers {
internal static class TestUtil { internal static class TestUtil {
...@@ -1577,6 +1579,20 @@ namespace Google.ProtocolBuffers { ...@@ -1577,6 +1579,20 @@ namespace Google.ProtocolBuffers {
} }
return goldenPackedFieldsMessage; return goldenPackedFieldsMessage;
} }
private static readonly string[] TestCultures = { "en-US", "en-GB", "fr-FR", "de-DE" };
public static void TestInMultipleCultures(Action test) {
CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture;
foreach (string culture in TestCultures) {
try {
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
test();
} finally {
Thread.CurrentThread.CurrentCulture = originalCulture;
}
}
}
/// <summary> /// <summary>
/// Helper to construct a byte array from a bunch of bytes. /// Helper to construct a byte array from a bunch of bytes.
......
...@@ -34,6 +34,8 @@ using System.IO; ...@@ -34,6 +34,8 @@ using System.IO;
using System.Text; using System.Text;
using Google.ProtocolBuffers.TestProtos; using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework; using NUnit.Framework;
using System.Globalization;
using System.Threading;
namespace Google.ProtocolBuffers { namespace Google.ProtocolBuffers {
[TestFixture] [TestFixture]
...@@ -82,8 +84,10 @@ namespace Google.ProtocolBuffers { ...@@ -82,8 +84,10 @@ namespace Google.ProtocolBuffers {
/// </summary> /// </summary>
[Test] [Test]
public void PrintMessage() { public void PrintMessage() {
string text = TextFormat.PrintToString(TestUtil.GetAllSet()); TestUtil.TestInMultipleCultures(() => {
Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n")); string text = TextFormat.PrintToString(TestUtil.GetAllSet());
Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
});
} }
/// <summary> /// <summary>
...@@ -213,9 +217,11 @@ namespace Google.ProtocolBuffers { ...@@ -213,9 +217,11 @@ namespace Google.ProtocolBuffers {
[Test] [Test]
public void Parse() { public void Parse() {
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); TestUtil.TestInMultipleCultures(() => {
TextFormat.Merge(AllFieldsSetText, builder); TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
TestUtil.AssertAllFieldsSet(builder.Build()); TextFormat.Merge(AllFieldsSetText, builder);
TestUtil.AssertAllFieldsSet(builder.Build());
});
} }
[Test] [Test]
......
...@@ -422,10 +422,10 @@ namespace Google.ProtocolBuffers.Descriptors { ...@@ -422,10 +422,10 @@ namespace Google.ProtocolBuffers.Descriptors {
defaultValue = TextFormat.ParseUInt64(Proto.DefaultValue); defaultValue = TextFormat.ParseUInt64(Proto.DefaultValue);
break; break;
case FieldType.Float: case FieldType.Float:
defaultValue = float.Parse(Proto.DefaultValue); defaultValue = TextFormat.ParseFloat(Proto.DefaultValue);
break; break;
case FieldType.Double: case FieldType.Double:
defaultValue = double.Parse(Proto.DefaultValue); defaultValue = TextFormat.ParseDouble(Proto.DefaultValue);
break; break;
case FieldType.Bool: case FieldType.Bool:
if (Proto.DefaultValue == "true") { if (Proto.DefaultValue == "true") {
......
...@@ -231,6 +231,41 @@ namespace Google.ProtocolBuffers { ...@@ -231,6 +231,41 @@ namespace Google.ProtocolBuffers {
return (int) ParseInteger(text, true, false); return (int) ParseInteger(text, true, false);
} }
internal static float ParseFloat(string text) {
switch (text) {
case "-inf":
case "-infinity":
case "-inff":
case "-infinityf":
return float.NegativeInfinity;
case "inf":
case "infinity":
case "inff":
case "infinityf":
return float.PositiveInfinity;
case "nan":
case "nanf":
return float.NaN;
default:
return float.Parse(text, CultureInfo.InvariantCulture);
}
}
internal static double ParseDouble(string text) {
switch (text) {
case "-inf":
case "-infinity":
return double.NegativeInfinity;
case "inf":
case "infinity":
return double.PositiveInfinity;
case "nan":
return double.NaN;
default:
return double.Parse(text, CultureInfo.InvariantCulture);
}
}
/// <summary> /// <summary>
/// Parses an integer in hex (leading 0x), decimal (no prefix) or octal (leading 0). /// Parses an integer in hex (leading 0x), decimal (no prefix) or octal (leading 0).
/// Only a negative sign is permitted, and it must come before the radix indicator. /// Only a negative sign is permitted, and it must come before the radix indicator.
......
...@@ -293,7 +293,6 @@ namespace Google.ProtocolBuffers { ...@@ -293,7 +293,6 @@ namespace Google.ProtocolBuffers {
/// Otherwise, throw a FormatException. /// Otherwise, throw a FormatException.
/// </summary> /// </summary>
public float ConsumeFloat() { public float ConsumeFloat() {
// We need to parse infinity and nan separately because // We need to parse infinity and nan separately because
// Float.parseFloat() does not accept "inf", "infinity", or "nan". // Float.parseFloat() does not accept "inf", "infinity", or "nan".
if (FloatInfinity.IsMatch(currentToken)) { if (FloatInfinity.IsMatch(currentToken)) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment