From f5c5ed13ffc9c90e8f803b5cc86aa72c1da87196 Mon Sep 17 00:00:00 2001 From: Maryam Ariyan Date: Thu, 20 Jun 2019 10:49:32 -0700 Subject: [PATCH] [Tests]: JsonSerializer.ToString serialization tests containing Enum (dotnet/corefx#38531) Commit migrated from https://github.com/dotnet/corefx/commit/046dbada036d786f420a7775ebcfd51a2a777add --- .../tests/Serialization/EnumTests.cs | 111 ++++++++++++++++++--- .../Serialization/TestClasses.SimpleTestClass.cs | 10 +- .../tests/Serialization/TestClasses.cs | 51 +++++++--- 3 files changed, 136 insertions(+), 36 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs b/src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs index 2df4405..c0aad42 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/EnumTests.cs @@ -275,9 +275,9 @@ namespace System.Text.Json.Serialization.Tests { string json = $"{{ \"MyByteEnum\" : {byte.MaxValue}, \"MyUInt32Enum\" : {UInt32.MaxValue}, \"MyUInt64Enum\" : {ulong.MaxValue} }}"; SimpleTestClass result = JsonSerializer.Parse(json); - Assert.Equal((SampleByteEnum)byte.MaxValue, result.MyByteEnum); - Assert.Equal((SampleUInt32Enum)UInt32.MaxValue, result.MyUInt32Enum); - Assert.Equal((SampleUInt64Enum)ulong.MaxValue, result.MyUInt64Enum); + Assert.Equal((SampleEnumByte)byte.MaxValue, result.MyByteEnum); + Assert.Equal((SampleEnumUInt32)UInt32.MaxValue, result.MyUInt32Enum); + Assert.Equal((SampleEnumUInt64)ulong.MaxValue, result.MyUInt64Enum); Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyEnum\" : {ulong.MaxValue} }}")); Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyByteEnum\" : {ulong.MaxValue} }}")); @@ -291,11 +291,11 @@ namespace System.Text.Json.Serialization.Tests [Fact] public static void Parse_MaxValuePlus1_QuotedVsNotQuoted_Throws() { - Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyByteEnum\" : {(ulong)byte.MaxValue + 1}, \"MyUInt32Enum\" : {(ulong)UInt32.MaxValue + 1} }}")); + Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyByteEnum\" : {ByteMaxPlus1}, \"MyUInt32Enum\" : {UInt32MaxPlus1} }}")); // Quoted throws - Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyByteEnum\" : \"{(ulong)byte.MaxValue + 1}\" }}")); - Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyUInt32Enum\" : \"{(ulong)UInt32.MaxValue + 1}\" }}")); + Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyByteEnum\" : \"{ByteMaxPlus1}\" }}")); + Assert.Throws(() => JsonSerializer.Parse($"{{ \"MyUInt32Enum\" : \"{UInt32MaxPlus1}\" }}")); } [Fact] @@ -321,11 +321,11 @@ namespace System.Text.Json.Serialization.Tests } [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_InvalidNumber_Throw(ulong i, ulong j, SampleByteEnum e1, SampleUInt32Enum e2) + [InlineData((ulong)byte.MaxValue + 1, (ulong)UInt32.MaxValue + 1, (SampleEnumByte)0, (SampleEnumUInt32)0)] + [InlineData((ulong)byte.MaxValue + 2, (ulong)UInt32.MaxValue + 2, (SampleEnumByte)1, (SampleEnumUInt32)1)] + [InlineData((ulong)byte.MaxValue + 13, (ulong)UInt32.MaxValue + 13, (SampleEnumByte)12, (SampleEnumUInt32)12)] + [InlineData((ulong)byte.MaxValue * 2, (ulong)UInt32.MaxValue * 2, (SampleEnumByte)byte.MaxValue - 1, (SampleEnumUInt32)UInt32.MaxValue - 1)] + public static void Parse_Ulong_InvalidNumber_Throw(ulong i, ulong j, SampleEnumByte e1, SampleEnumUInt32 e2) { string json = $"{{ \"MyByteEnum\" : {i}, \"MyUInt32Enum\" : {j} }}"; Assert.Throws(() => JsonSerializer.Parse(json)); @@ -357,21 +357,21 @@ namespace System.Text.Json.Serialization.Tests public static void EnumAsInt64Min() { SimpleTestClass obj = JsonSerializer.Parse(s_jsonInt64EnumMin); - Assert.Equal(SampleInt64Enum.Min, obj.MyInt64Enum); + Assert.Equal(SampleEnumInt64.MinNegative, obj.MyInt64Enum); } [Fact] public static void EnumAsInt64Max() { SimpleTestClass obj = JsonSerializer.Parse(s_jsonInt64EnumMax); - Assert.Equal(SampleInt64Enum.Max, obj.MyInt64Enum); + Assert.Equal(SampleEnumInt64.Max, obj.MyInt64Enum); } [Fact] public static void EnumAsUInt64Max() { SimpleTestClass obj = JsonSerializer.Parse(s_jsonUInt64EnumMax); - Assert.Equal(SampleUInt64Enum.Max, obj.MyUInt64Enum); + Assert.Equal(SampleEnumUInt64.Max, obj.MyUInt64Enum); } [Fact] @@ -385,14 +385,93 @@ namespace System.Text.Json.Serialization.Tests public static void EnumAsByteMax() { SimpleTestClass obj = JsonSerializer.Parse(s_jsonByteEnum); - Assert.Equal(SampleByteEnum.Max, obj.MyByteEnum); + Assert.Equal(SampleEnumByte.Max, obj.MyByteEnum); } [Fact] public static void EnumAsSByteMax() { SimpleTestClass obj = JsonSerializer.Parse(s_jsonSByteEnum); - Assert.Equal(SampleSByteEnum.Max, obj.MySByteEnum); + Assert.Equal(SampleEnumSByte.Max, obj.MySByteEnum); + } + + [Theory] + [InlineData(sbyte.MinValue, SampleEnumSByte.MinNegative)] + [InlineData(short.MinValue, SampleEnumInt16.MinNegative)] + [InlineData(int.MinValue, SampleEnumInt32.MinNegative)] + [InlineData(long.MinValue, SampleEnumInt64.MinNegative)] + [InlineData(0, SampleEnum.MinZero)] + [InlineData(0, SampleEnumByte.MinZero)] + [InlineData(0, SampleEnumUInt16.MinZero)] + [InlineData(0, SampleEnumUInt32.MinZero)] + [InlineData(0, SampleEnumUInt64.MinZero)] + [InlineData(0, SampleEnumSByte.Zero)] + [InlineData(0, SampleEnumInt16.Zero)] + [InlineData(0, SampleEnumInt32.Zero)] + [InlineData(0, SampleEnumInt64.Zero)] + [InlineData(1, SampleEnum.One)] + [InlineData(1, SampleEnumByte.One)] + [InlineData(1, SampleEnumSByte.One)] + [InlineData(1, SampleEnumInt16.One)] + [InlineData(1, SampleEnumUInt16.One)] + [InlineData(1, SampleEnumInt32.One)] + [InlineData(1, SampleEnumUInt32.One)] + [InlineData(1, SampleEnumInt64.One)] + [InlineData(1, SampleEnumUInt64.One)] + [InlineData(2, SampleEnum.Two)] + [InlineData(2, SampleEnumByte.Two)] + [InlineData(2, SampleEnumSByte.Two)] + [InlineData(2, SampleEnumInt16.Two)] + [InlineData(2, SampleEnumUInt16.Two)] + [InlineData(2, SampleEnumInt32.Two)] + [InlineData(2, SampleEnumUInt32.Two)] + [InlineData(2, SampleEnumInt64.Two)] + [InlineData(2, SampleEnumUInt64.Two)] + [InlineData(byte.MaxValue, SampleEnumByte.Max)] + [InlineData(sbyte.MaxValue, SampleEnumSByte.Max)] + [InlineData(short.MaxValue, SampleEnumInt16.Max)] + [InlineData(ushort.MaxValue, SampleEnumUInt16.Max)] + [InlineData(int.MaxValue, SampleEnumInt32.Max)] + [InlineData(uint.MaxValue, SampleEnumUInt32.Max)] + [InlineData(long.MaxValue, SampleEnumInt64.Max)] + [InlineData(ulong.MaxValue, SampleEnumUInt64.Max)] + public static void ToString_Max(object expected, object enumValue) + { + string json = JsonSerializer.ToString(enumValue); + Assert.Equal(expected.ToString(), json); + } + + [Theory] + [InlineData(1, (long)byte.MaxValue + 1, typeof(SampleEnumByte), SampleEnumByte.MinZero)] + [InlineData(2, (long)byte.MaxValue + 1, typeof(SampleEnumByte), SampleEnumByte.MinZero)] + [InlineData(3, (long)byte.MaxValue + 1, typeof(SampleEnumByte), SampleEnumByte.MinZero)] + [InlineData(4, (long)byte.MaxValue + 1, typeof(SampleEnumByte), SampleEnumByte.MinZero)] + [InlineData(1, (long)UInt16.MaxValue + 1, typeof(SampleEnumUInt16), SampleEnumUInt16.MinZero)] + [InlineData(2, (long)UInt16.MaxValue + 1, typeof(SampleEnumUInt16), SampleEnumUInt16.MinZero)] + [InlineData(3, (long)UInt16.MaxValue + 1, typeof(SampleEnumUInt16), SampleEnumUInt16.MinZero)] + [InlineData(4, (long)UInt16.MaxValue + 1, typeof(SampleEnumUInt16), SampleEnumUInt16.MinZero)] + [InlineData(1, (long)UInt32.MaxValue + 1, typeof(SampleEnumUInt32), SampleEnumUInt32.MinZero)] + [InlineData(2, (long)UInt32.MaxValue + 1, typeof(SampleEnumUInt32), SampleEnumUInt32.MinZero)] + [InlineData(3, (long)UInt32.MaxValue + 1, typeof(SampleEnumUInt32), SampleEnumUInt32.MinZero)] + [InlineData(4, (long)UInt32.MaxValue + 1, typeof(SampleEnumUInt32), SampleEnumUInt32.MinZero)] + [InlineData(1, (long)sbyte.MaxValue + 1, typeof(SampleEnumSByte), SampleEnumSByte.MinNegative)] + [InlineData(2, (long)sbyte.MaxValue + 1, typeof(SampleEnumSByte), SampleEnumSByte.Zero)] + [InlineData(3, (long)sbyte.MaxValue + 1, typeof(SampleEnumSByte), SampleEnumSByte.MinNegative)] + [InlineData(4, (long)sbyte.MaxValue + 1, typeof(SampleEnumSByte), SampleEnumSByte.Zero)] + [InlineData(1, (long)Int16.MaxValue + 1, typeof(SampleEnumInt16), SampleEnumInt16.MinNegative)] + [InlineData(2, (long)Int16.MaxValue + 1, typeof(SampleEnumInt16), SampleEnumInt16.Zero)] + [InlineData(3, (long)Int16.MaxValue + 1, typeof(SampleEnumInt16), SampleEnumInt16.MinNegative)] + [InlineData(4, (long)Int16.MaxValue + 1, typeof(SampleEnumInt16), SampleEnumInt16.Zero)] + [InlineData(1, (long)Int32.MaxValue + 1, typeof(SampleEnumInt32), SampleEnumInt32.MinNegative)] + [InlineData(2, (long)Int32.MaxValue + 1, typeof(SampleEnumInt32), SampleEnumInt32.Zero)] + [InlineData(3, (long)Int32.MaxValue + 1, typeof(SampleEnumInt32), SampleEnumInt32.MinNegative)] + [InlineData(4, (long)Int32.MaxValue + 1, typeof(SampleEnumInt32), SampleEnumInt32.Zero)] + public static void ToString_ExceedMaxCapacity_ResetsBackToMinimum(int timesOverflow, long maxCapacity, Type type, long expected) + { + object enumValue = Enum.ToObject(type, maxCapacity * timesOverflow); + string json = JsonSerializer.ToString(enumValue); + string expectedString = expected.ToString(); + Assert.Equal(expectedString, json); } } } diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs index 2d5551e..3ca4288 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.SimpleTestClass.cs @@ -29,14 +29,14 @@ namespace System.Text.Json.Serialization.Tests public double MyDouble { get; set; } public DateTime MyDateTime { get; set; } public DateTimeOffset MyDateTimeOffset { get; set; } - public SampleSByteEnum MySByteEnum { get; set; } - public SampleByteEnum MyByteEnum { get; set; } + public SampleEnumSByte MySByteEnum { get; set; } + public SampleEnumByte MyByteEnum { get; set; } public SampleEnum MyEnum { get; set; } public SampleEnumInt16 MyInt16Enum { get; set; } - public SampleInt64Enum MyInt64Enum { get; set; } - public SampleUInt32Enum MyUInt32Enum { get; set; } + public SampleEnumInt64 MyInt64Enum { get; set; } + public SampleEnumUInt32 MyUInt32Enum { get; set; } public SampleEnumUInt16 MyUInt16Enum { get; set; } - public SampleUInt64Enum MyUInt64Enum { get; set; } + public SampleEnumUInt64 MyUInt64Enum { get; set; } public short[] MyInt16Array { get; set; } public int[] MyInt32Array { get; set; } public long[] MyInt64Array { get; set; } diff --git a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs index ed9f6ba..3f9a939 100644 --- a/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs +++ b/src/libraries/System.Text.Json/tests/Serialization/TestClasses.cs @@ -15,28 +15,34 @@ namespace System.Text.Json.Serialization.Tests void Verify(); } - public enum SampleByteEnum : byte + public enum SampleEnumByte : byte { - One = 0, - Two = 1, + MinZero = byte.MinValue, + One = 1, + Two = 2, Max = byte.MaxValue } - public enum SampleSByteEnum : sbyte + public enum SampleEnumSByte : sbyte { - One = 0, - Two = 1, + MinNegative = sbyte.MinValue, + Zero = 0, + One = 1, + Two = 2, Max = sbyte.MaxValue } public enum SampleEnum { + MinZero = 0, One = 1, Two = 2 } public enum SampleEnumInt16 : short { + MinNegative = short.MinValue, + Zero = 0, One = 1, Two = 2, Max = short.MaxValue @@ -44,28 +50,43 @@ namespace System.Text.Json.Serialization.Tests public enum SampleEnumUInt16 : ushort { + MinZero = ushort.MinValue, One = 1, Two = 2, Max = ushort.MaxValue } - public enum SampleInt64Enum : long + public enum SampleEnumInt32 : Int32 { - Min = long.MinValue, - Max = long.MaxValue + MinNegative = Int32.MinValue, + Zero = 0, + One = 1, + Two = 2, + Max = Int32.MaxValue } - public enum SampleUInt32Enum : UInt32 + public enum SampleEnumUInt32 : UInt32 { - One = 0, - Two = 1, + MinZero = UInt32.MinValue, + One = 1, + Two = 2, Max = UInt32.MaxValue } - public enum SampleUInt64Enum : ulong + public enum SampleEnumInt64 : long { - One = 0, - Two = 1, + MinNegative = long.MinValue, + Zero = 0, + One = 1, + Two = 2, + Max = long.MaxValue + } + + public enum SampleEnumUInt64 : ulong + { + MinZero = ulong.MinValue, + One = 1, + Two = 2, Max = ulong.MaxValue } -- 2.7.4