Skip to content

Commit

Permalink
Improve code coverage: Struct, value type test (dotnet#38403)
Browse files Browse the repository at this point in the history
Code coverage: Struct, value type tests
  • Loading branch information
buyaa-n authored Jun 21, 2019
1 parent 620d199 commit c181ec7
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/System.Text.Json/tests/Serialization/Array.ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ public static void ReadPrimitiveArray()
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()
{
Expand Down
64 changes: 64 additions & 0 deletions src/System.Text.Json/tests/Serialization/Object.ReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -243,5 +244,68 @@ public static void ReadObject_PublicIndexer()
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public class SimpleTestClass : ITestClass
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; }
Expand Down Expand Up @@ -122,14 +124,18 @@ public class SimpleTestClass : ITestClass
@"""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]," +
Expand Down Expand Up @@ -206,7 +212,8 @@ public void Initialize()
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 };
Expand All @@ -227,6 +234,8 @@ public void Initialize()
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 };
Expand Down Expand Up @@ -319,6 +328,13 @@ public void Verify()
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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class SimpleTestClassWithSimpleObject : ITestClass
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 =
@"{" +
Expand All @@ -46,7 +47,8 @@ public class SimpleTestClassWithSimpleObject : ITestClass
@"""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);
Expand Down Expand Up @@ -74,6 +76,7 @@ public virtual void Initialize()
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()
Expand Down Expand Up @@ -139,6 +142,8 @@ public virtual void Verify()
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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Text.Json.Serialization.Tests
{
public class SimpleTestStruct : ITestClass
public struct SimpleTestStruct : ITestClass
{
public short MyInt16 { get; set; }
public int MyInt32 { get; set; }
Expand All @@ -28,6 +28,10 @@ public class SimpleTestStruct : ITestClass
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; }
Expand Down Expand Up @@ -75,7 +79,10 @@ public class SimpleTestStruct : ITestClass
@"""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]," +
Expand Down Expand Up @@ -126,6 +133,9 @@ public void Initialize()
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 };
Expand Down Expand Up @@ -175,6 +185,10 @@ public void Verify()
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]);
Expand Down
60 changes: 60 additions & 0 deletions src/System.Text.Json/tests/Serialization/TestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ public enum SampleEnumUInt64 : ulong
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; }
Expand Down Expand Up @@ -597,6 +632,31 @@ public void Verify()
}
}

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; }
Expand Down

0 comments on commit c181ec7

Please sign in to comment.