Fixed i18n issues with numbers
This commit is contained in:
parent
7a66a6d3f4
commit
d33bff04b3
6 changed files with 72 additions and 12 deletions
|
@ -228,8 +228,10 @@ namespace Google.ProtocolBuffers {
|
|||
|
||||
[Test]
|
||||
public void ReflectionDefaults() {
|
||||
reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
|
||||
reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
|
||||
TestUtil.TestInMultipleCultures(() => {
|
||||
reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
|
||||
reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
|
||||
});
|
||||
}
|
||||
// =================================================================
|
||||
// Extensions.
|
||||
|
@ -296,8 +298,10 @@ namespace Google.ProtocolBuffers {
|
|||
|
||||
[Test]
|
||||
public void ExtensionReflectionDefaults() {
|
||||
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
|
||||
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
|
||||
TestUtil.TestInMultipleCultures(() => {
|
||||
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
|
||||
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
@ -31,10 +31,12 @@
|
|||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Google.ProtocolBuffers.TestProtos;
|
||||
using NUnit.Framework;
|
||||
using System.Threading;
|
||||
|
||||
namespace Google.ProtocolBuffers {
|
||||
internal static class TestUtil {
|
||||
|
@ -1577,6 +1579,20 @@ namespace Google.ProtocolBuffers {
|
|||
}
|
||||
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>
|
||||
/// Helper to construct a byte array from a bunch of bytes.
|
||||
|
|
|
@ -34,6 +34,8 @@ using System.IO;
|
|||
using System.Text;
|
||||
using Google.ProtocolBuffers.TestProtos;
|
||||
using NUnit.Framework;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
|
||||
namespace Google.ProtocolBuffers {
|
||||
[TestFixture]
|
||||
|
@ -82,8 +84,10 @@ namespace Google.ProtocolBuffers {
|
|||
/// </summary>
|
||||
[Test]
|
||||
public void PrintMessage() {
|
||||
string text = TextFormat.PrintToString(TestUtil.GetAllSet());
|
||||
Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
|
||||
TestUtil.TestInMultipleCultures(() => {
|
||||
string text = TextFormat.PrintToString(TestUtil.GetAllSet());
|
||||
Assert.AreEqual(AllFieldsSetText.Replace("\r\n", "\n"), text.Replace("\r\n", "\n"));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -213,9 +217,11 @@ namespace Google.ProtocolBuffers {
|
|||
|
||||
[Test]
|
||||
public void Parse() {
|
||||
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
||||
TextFormat.Merge(AllFieldsSetText, builder);
|
||||
TestUtil.AssertAllFieldsSet(builder.Build());
|
||||
TestUtil.TestInMultipleCultures(() => {
|
||||
TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
|
||||
TextFormat.Merge(AllFieldsSetText, builder);
|
||||
TestUtil.AssertAllFieldsSet(builder.Build());
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
|
@ -422,10 +422,10 @@ namespace Google.ProtocolBuffers.Descriptors {
|
|||
defaultValue = TextFormat.ParseUInt64(Proto.DefaultValue);
|
||||
break;
|
||||
case FieldType.Float:
|
||||
defaultValue = float.Parse(Proto.DefaultValue);
|
||||
defaultValue = TextFormat.ParseFloat(Proto.DefaultValue);
|
||||
break;
|
||||
case FieldType.Double:
|
||||
defaultValue = double.Parse(Proto.DefaultValue);
|
||||
defaultValue = TextFormat.ParseDouble(Proto.DefaultValue);
|
||||
break;
|
||||
case FieldType.Bool:
|
||||
if (Proto.DefaultValue == "true") {
|
||||
|
|
|
@ -231,6 +231,41 @@ namespace Google.ProtocolBuffers {
|
|||
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>
|
||||
/// 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.
|
||||
|
|
|
@ -293,7 +293,6 @@ namespace Google.ProtocolBuffers {
|
|||
/// Otherwise, throw a FormatException.
|
||||
/// </summary>
|
||||
public float ConsumeFloat() {
|
||||
|
||||
// We need to parse infinity and nan separately because
|
||||
// Float.parseFloat() does not accept "inf", "infinity", or "nan".
|
||||
if (FloatInfinity.IsMatch(currentToken)) {
|
||||
|
|
Loading…
Add table
Reference in a new issue