Skip to content

Commit

Permalink
[Tests]: JsonSerializer.Parse for json strings containing Enum (dotne…
Browse files Browse the repository at this point in the history
…t/corefx#37895)

* Adding tests for Parsing json strings contains Enums

Related to: dotnet/corefx#37735

* Broke down new tests to make more readable

* nit + Add ActiveIssue test for bug found


Commit migrated from dotnet/corefx@af8d3ee
  • Loading branch information
maryamariyan authored Jun 8, 2019
1 parent 01b4092 commit ea00fba
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,99 @@ public static class EnumTests
@"""MyUInt64Enum"" : " + ulong.MaxValue +
@"}";

private static readonly string s_jsonByteEnum =
@"{" +
@"""MyByteEnum"" : " + byte.MaxValue +
@"}";

private const string UlongMaxPlus1 = "18446744073709551616"; // ulong.MaxValue + 1;

[Fact]
public static void Parse_UlongMaxPlus1_Throws()
{
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyEnum\" : {UlongMaxPlus1} }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : {UlongMaxPlus1} }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt32Enum\" : {UlongMaxPlus1} }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt64Enum\" : {UlongMaxPlus1} }}"));

Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyEnum\" : \"{UlongMaxPlus1}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : \"{UlongMaxPlus1}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt32Enum\" : \"{UlongMaxPlus1}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt64Enum\" : \"{UlongMaxPlus1}\" }}"));
}

[Fact]
public static void Parse_MaxValues_QuotedVsNotQuoted_QuotedThrows()
{
string json = $"{{ \"MyByteEnum\" : {byte.MaxValue}, \"MyUInt32Enum\" : {UInt32.MaxValue}, \"MyUInt64Enum\" : {ulong.MaxValue} }}";
SimpleTestClass result = JsonSerializer.Parse<SimpleTestClass>(json);
Assert.Equal((SampleByteEnum)byte.MaxValue, result.MyByteEnum);
Assert.Equal((SampleUInt32Enum)UInt32.MaxValue, result.MyUInt32Enum);
Assert.Equal((SampleUInt64Enum)ulong.MaxValue, result.MyUInt64Enum);

Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyEnum\" : {ulong.MaxValue} }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : {ulong.MaxValue} }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt32Enum\" : {ulong.MaxValue} }}"));

Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyEnum\" : \"{ulong.MaxValue}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : \"{ulong.MaxValue}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt32Enum\" : \"{ulong.MaxValue}\" }}"));
}

[Fact]
public static void Parse_MaxValuePlus1_QuotedVsNotQuoted_QuotedThrows()
{
SimpleTestClass result = JsonSerializer.Parse<SimpleTestClass>(
$"{{ \"MyByteEnum\" : {(ulong)byte.MaxValue + 1}, \"MyUInt32Enum\" : {(ulong)UInt32.MaxValue + 1} }}");
Assert.Equal((SampleByteEnum)0, result.MyByteEnum);
Assert.Equal((SampleUInt32Enum)0, result.MyUInt32Enum);

// Quoted throws
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : \"{(ulong)byte.MaxValue + 1}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyUInt32Enum\" : \"{(ulong)UInt32.MaxValue + 1}\" }}"));
}

[Fact]
public static void Parse_Negative1_QuotedVsUnquoted_QuotedThrows()
{
string json = "{ \"MyByteEnum\" : -1, \"MyUInt32Enum\" : -1 }";
SimpleTestClass result = JsonSerializer.Parse<SimpleTestClass>(json);
Assert.Equal((SampleByteEnum)byte.MaxValue, result.MyByteEnum);
Assert.Equal((SampleUInt32Enum)UInt32.MaxValue, result.MyUInt32Enum);

// Quoted throws
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>("{ \"MyByteEnum\" : \"-1\" }"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>("{ \"MyUInt32Enum\" : \"-1\" }"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>("{ \"MyUInt64Enum\" : \"-1\" }"));
}

[Theory]
[InlineData("{ \"MyUInt64Enum\" : -1 }")]
[InlineData("{ \"MyUInt64Enum\" : -1, \"MyUInt32Enum\" : -1 }")]
[InlineData("{ \"MyUInt64Enum\" : -1, \"MyUInt32Enum\" : -1, \"MyByteEnum\" : -1 }")]
[ActiveIssue(38363)]
public static void Parse_Negative1ForUInt64Enum_ShouldNotThrow(string json)
{
SimpleTestClass result = JsonSerializer.Parse<SimpleTestClass>(json);
Assert.Equal((SampleUInt64Enum)UInt64.MaxValue, result.MyUInt64Enum);
}

[Theory]
[InlineData((ulong)byte.MaxValue + 1, (ulong)UInt32.MaxValue + 1, (SampleByteEnum)0, (SampleUInt32Enum)0)]
[InlineData((ulong)byte.MaxValue + 2, (ulong)UInt32.MaxValue + 2, (SampleByteEnum)1, (SampleUInt32Enum)1)]
[InlineData((ulong)byte.MaxValue + 13, (ulong)UInt32.MaxValue + 13, (SampleByteEnum)12, (SampleUInt32Enum)12)]
[InlineData((ulong)byte.MaxValue * 2, (ulong)UInt32.MaxValue * 2, (SampleByteEnum)byte.MaxValue - 1, (SampleUInt32Enum)UInt32.MaxValue - 1)]
public static void Parse_Ulong_JsonWithAcceptableInvalidNumber_Success(ulong i, ulong j, SampleByteEnum e1, SampleUInt32Enum e2)
{
string json = $"{{ \"MyByteEnum\" : {i}, \"MyUInt32Enum\" : {j} }}";
SimpleTestClass result = JsonSerializer.Parse<SimpleTestClass>(json);
Assert.Equal(e1, result.MyByteEnum);
Assert.Equal(e2, result.MyUInt32Enum);

Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyEnum\" : \"{i}\" }}"));
Assert.Throws<JsonException>(() => JsonSerializer.Parse<SimpleTestClass>($"{{ \"MyByteEnum\" : \"{j}\" }}"));
}

[Fact]
public static void EnumAsStringFail()
{
Expand Down Expand Up @@ -66,5 +159,12 @@ public static void EnumAsUInt64Max()
SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonUInt64EnumMax);
Assert.Equal(SampleUInt64Enum.Max, obj.MyUInt64Enum);
}

[Fact]
public static void EnumAsByteMax()
{
SimpleTestClass obj = JsonSerializer.Parse<SimpleTestClass>(s_jsonByteEnum);
Assert.Equal(SampleByteEnum.Max, obj.MyByteEnum);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public class SimpleTestClass : ITestClass
public double MyDouble { get; set; }
public DateTime MyDateTime { get; set; }
public DateTimeOffset MyDateTimeOffset { get; set; }
public SampleByteEnum MyByteEnum { get; set; }
public SampleEnum MyEnum { get; set; }
public SampleInt64Enum MyInt64Enum { get; set; }
public SampleUInt32Enum MyUInt32Enum { get; set; }
public SampleUInt64Enum MyUInt64Enum { get; set; }
public short[] MyInt16Array { get; set; }
public int[] MyInt32Array { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public interface ITestClass
void Verify();
}

public enum SampleByteEnum : byte
{
One = 0,
Two = 1,
Max = byte.MaxValue
}

public enum SampleEnum
{
One = 1,
Expand All @@ -27,8 +34,17 @@ public enum SampleInt64Enum : long
Max = long.MaxValue
}

public enum SampleUInt32Enum : UInt32
{
One = 0,
Two = 1,
Max = UInt32.MaxValue
}

public enum SampleUInt64Enum : ulong
{
One = 0,
Two = 1,
Max = ulong.MaxValue
}

Expand Down

0 comments on commit ea00fba

Please sign in to comment.