Skip to content

Commit

Permalink
End-to-end serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed Oct 19, 2022
1 parent f9d3bed commit febb3c4
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleTestClass>(@"{""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<SimpleTestClass>(@"{""myInt16"":1}", options);
private async Task DeserializeAndAssert(JsonNamingPolicy policy, string json, short expected)
{
var options = new JsonSerializerOptions { PropertyNamingPolicy = policy };
var obj = await Serializer.DeserializeWrapper<SimpleTestClass>(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<SimpleTestClass>(@"{}", options);
async Task SerializeAndAssert(JsonNamingPolicy policy, string myInt16, string myInt32)
{
var options = new JsonSerializerOptions { PropertyNamingPolicy = policy };
var obj = await Serializer.DeserializeWrapper<SimpleTestClass>(@"{}", 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]
Expand Down

0 comments on commit febb3c4

Please sign in to comment.