Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Nov 11, 2021
1 parent 90afe0c commit c685d79
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
4 changes: 0 additions & 4 deletions csharp/src/Google.Protobuf.Test/JsonParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,9 @@ public void NumberToDouble_Valid(string jsonValue, double expectedParsedValue)
}

[Test]
// Skip these test cases in .NET 5 because floating point parsing supports bigger values.
// These big values won't throw an error in the test.
#if !NETCOREAPP3_1_OR_GREATER
[TestCase("1.7977e308")]
[TestCase("-1.7977e308")]
[TestCase("1e309")]
#endif
[TestCase("1,0")]
[TestCase("1.0.0")]
[TestCase("+1")]
Expand Down
4 changes: 0 additions & 4 deletions csharp/src/Google.Protobuf.Test/JsonTokenizerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,8 @@ public void NumberValue(string json, double expectedValue)
[TestCase("1e-")]
[TestCase("--")]
[TestCase("--1")]
// Skip these test cases in .NET 5 because floating point parsing supports bigger values.
// These big values won't throw an error in the test.
#if !NETCOREAPP3_1_OR_GREATER
[TestCase("-1.7977e308")]
[TestCase("1.7977e308")]
#endif
public void InvalidNumberValue(string json)
{
AssertThrowsAfter(json);
Expand Down
11 changes: 10 additions & 1 deletion csharp/src/Google.Protobuf/JsonTokenizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,18 @@ private double ReadNumber(char initialCharacter)
// TODO: What exception should we throw if the value can't be represented as a double?
try
{
return double.Parse(builder.ToString(),
double result = double.Parse(builder.ToString(),
NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent,
CultureInfo.InvariantCulture);

// .NET Core 3.0 and later returns infinity if the number is too large or small to be represented.
// For compatibility with other Protobuf implementations the tokenizer should still throw.
if (double.IsInfinity(result))
{
throw reader.CreateException("Numeric value out of range: " + builder);
}

return result;
}
catch (OverflowException)
{
Expand Down

0 comments on commit c685d79

Please sign in to comment.