Improve code coverage: Struct, value type test (dotnet/corefx#38403)
authorbuyaa-n <bunamnan@microsoft.com>
Fri, 21 Jun 2019 20:02:44 +0000 (13:02 -0700)
committerGitHub <noreply@github.com>
Fri, 21 Jun 2019 20:02:44 +0000 (13:02 -0700)
Code coverage: Struct, value type tests

Commit migrated from https://github.com/dotnet/corefx/commit/c181ec76bbaf2d85883bef1328a7f61e5cbf8016

src/libraries/System.Text.Json/tests/Serialization/Array.ReadTests.cs
src/libraries/System.Text.Json/tests/Serialization/Object.ReadTests.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClassWithSimpleObject.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestStruct.cs
src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs

index 20480ff..2dd3692 100644 (file)
@@ -168,6 +168,18 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal(0, i.Length);
         }
 
+        [ActiveIssue(38435)]
+        [Fact]
+        public static void ReadInitializedArrayTest()
+        {
+            string serialized = "{\"Values\":[1,2,3]}";
+            TestClassWithInitializedArray testClassWithInitializedArray = JsonSerializer.Parse<TestClassWithInitializedArray>(serialized);
+
+            Assert.Equal(1, testClassWithInitializedArray.Values[0]);
+            Assert.Equal(2, testClassWithInitializedArray.Values[1]);
+            Assert.Equal(3, testClassWithInitializedArray.Values[2]);
+        }
+
         [Fact]
         public static void ReadArrayWithEnums()
         {
index e4ac85b..8a2e522 100644 (file)
@@ -2,6 +2,7 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
+using System.Collections;
 using System.Collections.Generic;
 using Xunit;
 
@@ -243,5 +244,68 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal("Value", indexer.NonIndexerProp);
             Assert.Equal(-1, indexer[0]);
         }
+
+        [ActiveIssue(38414)]
+        [Fact]
+        public static void ReadSimpleStructWithSimpleClass()
+        {
+            SimpleStructWithSimpleClass testObject = new SimpleStructWithSimpleClass();
+            testObject.Initialize();
+
+            string json = JsonSerializer.ToString(testObject, testObject.GetType());
+            SimpleStructWithSimpleClass obj = JsonSerializer.Parse<SimpleStructWithSimpleClass>(json);
+            obj.Verify();
+        }
+
+        [ActiveIssue(38490)]
+        [Fact]
+        public static void ReadSimpleTestStructWithSimpleTestClass()
+        {
+            SimpleTestStruct testObject = new SimpleTestStruct();
+            testObject.Initialize();
+            testObject.MySimpleTestClass = new SimpleTestClass  { MyString = "Hello", MyDouble = 3.14 } ;
+
+            string json = JsonSerializer.ToString(testObject);
+            SimpleTestStruct parsedObject = JsonSerializer.Parse<SimpleTestStruct>(json);
+            parsedObject.Verify();
+        }
+
+        [Fact]
+        public static void ReadSimpleTestClassWithSimpleTestStruct()
+        {
+            SimpleTestClass testObject = new SimpleTestClass();
+            testObject.Initialize();
+            testObject.MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } };
+            
+            string json = JsonSerializer.ToString(testObject);
+            SimpleTestClass parsedObject = JsonSerializer.Parse<SimpleTestClass>(json);
+            parsedObject.Verify();
+        }
+
+        [ActiveIssue(38414)]
+        [Fact]
+        public static void OuterClassHavingPropertiesDefinedAfterClassWithDictionaryTest()
+        {
+            OuterClassHavingPropertiesDefinedAfterClassWithDictionary testObject = new OuterClassHavingPropertiesDefinedAfterClassWithDictionary { MyInt = 10, MyIntArray = new int[] { 3 },
+                MyDouble= 3.14, MyList =new List<string> { "Hello" }, MyString = "World",  MyInnerTestClass = new SimpleClassWithDictionary { MyInt = 2 } };
+            string json = JsonSerializer.ToString(testObject);
+
+            OuterClassHavingPropertiesDefinedAfterClassWithDictionary parsedObject = JsonSerializer.Parse<OuterClassHavingPropertiesDefinedAfterClassWithDictionary>(json);
+
+            Assert.Equal(3.14, parsedObject.MyDouble);
+            Assert.Equal(10, parsedObject.MyInt);
+            Assert.Equal("World", parsedObject.MyString);
+            Assert.Equal("Hello", parsedObject.MyList[0]);
+            Assert.Equal(3, parsedObject.MyIntArray[0]);
+            Assert.Equal(2, parsedObject.MyInnerTestClass.MyInt);
+        }
+
+        [ActiveIssue(38435)]
+        [Fact]
+        public static void ReadStructWithSimpleClassValueTest()
+        {
+            string json = "{\"MySimpleTestClass\":{\"MyInt32Array\":[1],\"MyStringToStringDict\":null,\"MyStringToStringIDict\":null},\"MyInt32Array\":[2]}";
+            SimpleTestStruct obj3 = JsonSerializer.Parse<SimpleTestStruct>(json);
+        }
     }
 }
index 750222c..99a06eb 100644 (file)
@@ -36,9 +36,11 @@ namespace System.Text.Json.Serialization.Tests
         public SampleEnum MyEnum { get; set; }
         public SampleEnumInt16 MyInt16Enum { get; set; }
         public SampleEnumInt64 MyInt64Enum { get; set; }
-        public SampleEnumUInt32 MyUInt32Enum { get; set; }
         public SampleEnumUInt16 MyUInt16Enum { get; set; }
+        public SampleEnumUInt32 MyUInt32Enum { get; set; }
         public SampleEnumUInt64 MyUInt64Enum { get; set; }
+        public SimpleStruct MySimpleStruct { get; set; }
+        public SimpleTestStruct MySimpleTestStruct { get; set; }      
         public short[] MyInt16Array { get; set; }
         public int[] MyInt32Array { get; set; }
         public long[] MyInt64Array { get; set; }
@@ -122,14 +124,18 @@ namespace System.Text.Json.Serialization.Tests
                 @"""MyGuid"" : ""1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6""," +
                 @"""MyUri"" : ""https:\/\/github.com\/dotnet\/corefx""," +
                 @"""MyEnum"" : 2," + // int by default
-                //@"""MyStringToStringKeyValuePair"" : {""Key"" : ""myKey"", ""Value"" : ""myValue""}," +
+                @"""MyInt64Enum"" : -9223372036854775808," +
+                @"""MyUInt64Enum"" : 18446744073709551615," +
+                 //@"""MyStringToStringKeyValuePair"" : {""Key"" : ""myKey"", ""Value"" : ""myValue""}," +
                 @"""MyStringToStringIDict"" : {""key"" : ""value""}," +
                 @"""MyStringToStringGenericDict"" : {""key"" : ""value""}," +
                 @"""MyStringToStringGenericIDict"" : {""key"" : ""value""}," +
                 @"""MyStringToStringGenericIReadOnlyDict"" : {""key"" : ""value""}," +
                 @"""MyStringToStringImmutableDict"" : {""key"" : ""value""}," +
                 @"""MyStringToStringIImmutableDict"" : {""key"" : ""value""}," +
-                @"""MyStringToStringImmutableSortedDict"" : {""key"" : ""value""}";
+                @"""MyStringToStringImmutableSortedDict"" : {""key"" : ""value""}," +
+                @"""MySimpleStruct"" : {""One"" : 11, ""Two"" : 1.9999, ""Three"" : 33}," +
+                @"""MySimpleTestStruct"" : {""MyInt64"" : 64, ""MyString"" :""Hello"", ""MyInt32Array"" : [32]}";
 
         private const string s_partialJsonArrays =
                 @"""MyInt16Array"" : [1]," +
@@ -206,7 +212,8 @@ namespace System.Text.Json.Serialization.Tests
             MyGuid = new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6");
             MyUri = new Uri("https://github.com/dotnet/corefx");
             MyEnum = SampleEnum.Two;
-
+            MyInt64Enum = SampleEnumInt64.MinNegative;
+            MyUInt64Enum = SampleEnumUInt64.Max;
             MyInt16Array = new short[] { 1 };
             MyInt32Array = new int[] { 2 };
             MyInt64Array = new long[] { 3 };
@@ -227,6 +234,8 @@ namespace System.Text.Json.Serialization.Tests
             MyGuidArray = new Guid[] { new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6") };
             MyUriArray = new Uri[] { new Uri("https://github.com/dotnet/corefx") };
             MyEnumArray = new SampleEnum[] { SampleEnum.Two };
+            MySimpleStruct = new SimpleStruct { One = 11, Two = 1.9999 };
+            MySimpleTestStruct = new SimpleTestStruct { MyInt64 = 64, MyString = "Hello", MyInt32Array = new int[] { 32 } };
 
             MyInt16TwoDimensionArray = new int[2][];
             MyInt16TwoDimensionArray[0] = new int[] { 10, 11 };
@@ -319,6 +328,13 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal(new Guid("1B33498A-7B7D-4DDA-9C13-F6AA4AB449A6"), MyGuid);
             Assert.Equal(new Uri("https://github.com/dotnet/corefx"), MyUri);
             Assert.Equal(SampleEnum.Two, MyEnum);
+            Assert.Equal(SampleEnumInt64.MinNegative, MyInt64Enum);
+            Assert.Equal(SampleEnumUInt64.Max, MyUInt64Enum);
+            Assert.Equal(11, MySimpleStruct.One);
+            Assert.Equal(1.9999, MySimpleStruct.Two);
+            Assert.Equal(64, MySimpleTestStruct.MyInt64);
+            Assert.Equal("Hello", MySimpleTestStruct.MyString);
+            Assert.Equal(32, MySimpleTestStruct.MyInt32Array[0]);
 
             Assert.Equal((short)1, MyInt16Array[0]);
             Assert.Equal((int)2, MyInt32Array[0]);
index 5060fac..e5a0f52 100644 (file)
@@ -26,6 +26,7 @@ namespace System.Text.Json.Serialization.Tests
         public object MyDateTime { get; set; }
         public object MyGuid { get; set; }
         public object MyEnum { get; set; }
+        public object MyStruct { get; set; }
 
         public static readonly string s_json =
             @"{" +
@@ -46,7 +47,8 @@ namespace System.Text.Json.Serialization.Tests
                 @"""MyDecimal"" : 3.3," +
                 @"""MyDateTime"" : ""2019-01-30T12:01:02.0000000Z""," +
                 @"""MyGuid"" : ""5BB9D872-DA8A-471E-AA70-08E19102683D""," +
-                @"""MyEnum"" : 2" + // int by default
+                @"""MyEnum"" : 2," + // int by default
+                @"""MyStruct"" : { ""One"" : 1, ""Two"" : 3.14 }" +
             @"}";
 
         public static readonly byte[] s_data = Encoding.UTF8.GetBytes(s_json);
@@ -74,6 +76,7 @@ namespace System.Text.Json.Serialization.Tests
             MyDateTime = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
             MyGuid = new Guid("5BB9D872-DA8A-471E-AA70-08E19102683D");
             MyEnum = SampleEnum.Two;
+            MyStruct = new SimpleStruct { One = 1, Two = 3.14 };
         }
 
         public virtual void Verify()
@@ -139,6 +142,8 @@ namespace System.Text.Json.Serialization.Tests
             Assert.IsType<JsonElement>(MyEnum);
             Assert.Equal(JsonValueType.Number, ((JsonElement)MyEnum).Type);
             Assert.Equal(SampleEnum.Two, (SampleEnum)((JsonElement)MyEnum).GetUInt32());
+            Assert.Equal(1, ((JsonElement)MyStruct).GetProperty("One").GetInt32());
+            Assert.Equal(3.14, ((JsonElement)MyStruct).GetProperty("Two").GetDouble());
         }
     }
 }
index 0c7d9e1..a6a2341 100644 (file)
@@ -8,7 +8,7 @@ using Xunit;
 
 namespace System.Text.Json.Serialization.Tests
 {
-    public class SimpleTestStruct : ITestClass
+    public struct SimpleTestStruct : ITestClass
     {
         public short MyInt16 { get; set; }
         public int MyInt32 { get; set; }
@@ -28,6 +28,10 @@ namespace System.Text.Json.Serialization.Tests
         public DateTime MyDateTime { get; set; }
         public DateTimeOffset MyDateTimeOffset { get; set; }
         public SampleEnum MyEnum { get; set; }
+        public SampleEnumInt64 MyInt64Enum { get; set; }
+        public SampleEnumUInt64 MyUInt64Enum { get; set; }
+        public SimpleStruct MySimpleStruct { get; set; }
+        public SimpleTestClass MySimpleTestClass { get; set; }
         public short[] MyInt16Array { get; set; }
         public int[] MyInt32Array { get; set; }
         public long[] MyInt64Array { get; set; }
@@ -75,7 +79,10 @@ namespace System.Text.Json.Serialization.Tests
                 @"""MyDecimal"" : 3.3," +
                 @"""MyDateTime"" : ""2019-01-30T12:01:02.0000000Z""," +
                 @"""MyDateTimeOffset"" : ""2019-01-30T12:01:02.0000000+01:00""," +
-                @"""MyEnum"" : 2"; // int by default
+                @"""MyEnum"" : 2," + // int by default
+                @"""MyInt64Enum"" : -9223372036854775808," +
+                @"""MyUInt64Enum"" : 18446744073709551615," +
+                @"""MySimpleStruct"" : {""One"" : 11, ""Two"" : 1.9999, ""Three"" : 33}";
 
         private const string s_partialJsonArrays =
                 @"""MyInt16Array"" : [1]," +
@@ -126,6 +133,9 @@ namespace System.Text.Json.Serialization.Tests
             MyDateTime = new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc);
             MyDateTimeOffset = new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0));
             MyEnum = SampleEnum.Two;
+            MyInt64Enum = SampleEnumInt64.MinNegative;
+            MyUInt64Enum = SampleEnumUInt64.Max;
+            MySimpleStruct = new SimpleStruct { One = 11, Two = 1.9999 };
 
             MyInt16Array = new short[] { 1 };
             MyInt32Array = new int[] { 2 };
@@ -175,6 +185,10 @@ namespace System.Text.Json.Serialization.Tests
             Assert.Equal(new DateTime(2019, 1, 30, 12, 1, 2, DateTimeKind.Utc), MyDateTime);
             Assert.Equal(new DateTimeOffset(2019, 1, 30, 12, 1, 2, new TimeSpan(1, 0, 0)), MyDateTimeOffset);
             Assert.Equal(SampleEnum.Two, MyEnum);
+            Assert.Equal(SampleEnumInt64.MinNegative, MyInt64Enum);
+            Assert.Equal(SampleEnumUInt64.Max, MyUInt64Enum);
+            Assert.Equal(11, MySimpleStruct.One);
+            Assert.Equal(1.9999, MySimpleStruct.Two);
 
             Assert.Equal((short)1, MyInt16Array[0]);
             Assert.Equal((int)2, MyInt32Array[0]);
index 3f9a939..675b24a 100644 (file)
@@ -90,6 +90,41 @@ namespace System.Text.Json.Serialization.Tests
         Max = ulong.MaxValue
     }
 
+    public struct SimpleStruct
+    {
+        public int One { get; set; }
+        public double Two { get; set; }
+    }
+    
+    public struct SimpleStructWithSimpleClass: ITestClass
+    {
+        public short MyInt32 { get; set; }
+        public SimpleTestClass MySimpleClass { get; set; }
+        public int[] MyInt32Array { get; set; }
+
+        public static readonly string s_json =
+            @"{" +
+                @"""MySimpleClass"" : {""MyString"" : ""Hello"", ""MyDouble"" : 3.14}," +
+                @"""MyInt32"" : 32," +
+                @"""MyInt32Array"" : [32]" +
+            @"}";
+
+        public void Initialize()
+        {
+            MySimpleClass = new SimpleTestClass { MyString = "Hello", MyDouble = 3.14 };
+            MyInt32 = 32;
+            MyInt32Array = new int[] { 32 };
+        }
+
+        public void Verify()
+        {
+            Assert.Equal(32, MyInt32);
+            Assert.Equal(32, MyInt32Array[0]);
+            Assert.Equal("Hello", MySimpleClass.MyString);
+            Assert.Equal(3.14, MySimpleClass.MyDouble);
+        }
+    }
+
     public class TestClassWithNull
     {
         public string MyString { get; set; }
@@ -597,6 +632,31 @@ namespace System.Text.Json.Serialization.Tests
         }
     }
 
+    public class TestClassWithInitializedArray
+    {
+        public int[] Values { get; set; }
+
+        public TestClassWithInitializedArray()
+        {
+            Values = Array.Empty<int>();
+        }
+    }
+
+    public class SimpleClassWithDictionary
+    {
+        public int MyInt { get; set; }
+        public Dictionary<string, string> MyDictionary { get; set; }
+    }
+    public class OuterClassHavingPropertiesDefinedAfterClassWithDictionary
+    {
+        public double MyDouble { get; set; }
+        public SimpleClassWithDictionary MyInnerTestClass { get; set; }
+        public int MyInt { get; set; }
+        public string MyString { get; set; }
+        public List<string> MyList { get; set; }
+        public int[] MyIntArray { get; set; }
+    }
+
     public class TestClassWithStringArray : ITestClass
     {
         public string[] MyData { get; set; }