[Tests]: JsonSerializer.Parse for json strings containing Enum (dotnet/corefx#37895)
authorMaryam Ariyan <maryam.ariyan@microsoft.com>
Sat, 8 Jun 2019 16:50:19 +0000 (09:50 -0700)
committerGitHub <noreply@github.com>
Sat, 8 Jun 2019 16:50:19 +0000 (09:50 -0700)
* 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 https://github.com/dotnet/corefx/commit/af8d3eee98e018cb3fcbcf9d87f3d38725342dc2

src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs

index 79a376a..70e6a1a 100644 (file)
@@ -33,6 +33,99 @@ namespace System.Text.Json.Serialization.Tests
                 @"""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()
         {
@@ -66,5 +159,12 @@ namespace System.Text.Json.Serialization.Tests
             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);
+        }
     }
 }
index a79f244..c788fd2 100644 (file)
@@ -28,8 +28,10 @@ namespace System.Text.Json.Serialization.Tests
         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; }
index 7f88ab2..ef9dcc8 100644 (file)
@@ -15,6 +15,13 @@ namespace System.Text.Json.Serialization.Tests
         void Verify();
     }
 
+    public enum SampleByteEnum : byte
+    {
+        One = 0,
+        Two = 1,
+        Max = byte.MaxValue
+    }
+
     public enum SampleEnum
     {
         One = 1,
@@ -27,8 +34,17 @@ namespace System.Text.Json.Serialization.Tests
         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
     }