From febb3c4409249af7ec7e50804ccfd366575129be Mon Sep 17 00:00:00 2001 From: Yoh Deadfall Date: Wed, 19 Oct 2022 12:30:06 +0300 Subject: [PATCH] End-to-end serialization tests --- .../tests/Common/PropertyNameTests.cs | 56 ++++++++++++------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs b/src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs index 568357a5088b1..e20233a678f6c 100644 --- a/src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs @@ -15,40 +15,54 @@ public abstract partial class PropertyNameTests : SerializerTests public PropertyNameTests(JsonSerializerWrapper serializerWrapper) : base(serializerWrapper) { } [Fact] - public async Task CamelCaseDeserializeNoMatch() + public async Task BuiltInPolicyDeserializeNoMatch() { - var options = new JsonSerializerOptions(); - options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; - - SimpleTestClass obj = await Serializer.DeserializeWrapper(@"{""MyInt16"":1}", options); - - // This is 0 (default value) because the data does not match the property "MyInt16" that is assuming camel-casing of "myInt16". - Assert.Equal(0, obj.MyInt16); + // This is 0 (default value) because the data does not match the property "MyInt16" using the specified policy. + await DeserializeAndAssert(JsonNamingPolicy.CamelCase, @"{""MyInt16"":1}", 0); + await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"{""MyInt16"":1}", 0); + await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"{""MyInt16"":1}", 0); + await DeserializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"{""MyInt16"":1}", 0); + await DeserializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"{""MyInt16"":1}", 0); } [Fact] - public async Task CamelCaseDeserializeMatch() + public async Task BuiltInPolicyDeserializeMatch() { - var options = new JsonSerializerOptions(); - options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + // This is 1 because the data matches the property "MyInt16" using the specified policy. + await DeserializeAndAssert(JsonNamingPolicy.CamelCase, @"{""myInt16"":1}", 1); + await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"{""my_int16"":1}", 1); + await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"{""MY_INT16"":1}", 1); + await DeserializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"{""my-int16"":1}", 1); + await DeserializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"{""MY-INT16"":1}", 1); + } - SimpleTestClass obj = await Serializer.DeserializeWrapper(@"{""myInt16"":1}", options); + private async Task DeserializeAndAssert(JsonNamingPolicy policy, string json, short expected) + { + var options = new JsonSerializerOptions { PropertyNamingPolicy = policy }; + var obj = await Serializer.DeserializeWrapper(json, options); - // This is 1 because the data matches the property "MyInt16" that is assuming camel-casing of "myInt16". - Assert.Equal(1, obj.MyInt16); + Assert.Equal(expected, obj.MyInt16); } [Fact] - public async Task CamelCaseSerialize() + public async Task BuiltInPolicySerialize() { - var options = new JsonSerializerOptions(); - options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + await SerializeAndAssert(JsonNamingPolicy.CamelCase, @"""myInt16"":0", @"""myInt32"":0"); + await SerializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"""my_int16"":0", @"""my_int32"":0"); + await SerializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"""MY_INT16"":0", @"""MY_INT32"":0"); + await SerializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"""my-int16"":0", @"""my-int32"":0"); + await SerializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"""MY-INT16"":0", @"""MY-INT32"":0"); - SimpleTestClass obj = await Serializer.DeserializeWrapper(@"{}", options); + async Task SerializeAndAssert(JsonNamingPolicy policy, string myInt16, string myInt32) + { + var options = new JsonSerializerOptions { PropertyNamingPolicy = policy }; + var obj = await Serializer.DeserializeWrapper(@"{}", options); - string json = await Serializer.SerializeWrapper(obj, options); - Assert.Contains(@"""myInt16"":0", json); - Assert.Contains(@"""myInt32"":0", json); + string json = await Serializer.SerializeWrapper(obj, options); + + Assert.Contains(myInt16, json); + Assert.Contains(myInt32, json); + } } [Fact]