Skip to content

Commit febb3c4

Browse files
committed
End-to-end serialization tests
1 parent f9d3bed commit febb3c4

File tree

1 file changed

+35
-21
lines changed

1 file changed

+35
-21
lines changed

src/libraries/System.Text.Json/tests/Common/PropertyNameTests.cs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,54 @@ public abstract partial class PropertyNameTests : SerializerTests
1515
public PropertyNameTests(JsonSerializerWrapper serializerWrapper) : base(serializerWrapper) { }
1616

1717
[Fact]
18-
public async Task CamelCaseDeserializeNoMatch()
18+
public async Task BuiltInPolicyDeserializeNoMatch()
1919
{
20-
var options = new JsonSerializerOptions();
21-
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
22-
23-
SimpleTestClass obj = await Serializer.DeserializeWrapper<SimpleTestClass>(@"{""MyInt16"":1}", options);
24-
25-
// This is 0 (default value) because the data does not match the property "MyInt16" that is assuming camel-casing of "myInt16".
26-
Assert.Equal(0, obj.MyInt16);
20+
// This is 0 (default value) because the data does not match the property "MyInt16" using the specified policy.
21+
await DeserializeAndAssert(JsonNamingPolicy.CamelCase, @"{""MyInt16"":1}", 0);
22+
await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"{""MyInt16"":1}", 0);
23+
await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"{""MyInt16"":1}", 0);
24+
await DeserializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"{""MyInt16"":1}", 0);
25+
await DeserializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"{""MyInt16"":1}", 0);
2726
}
2827

2928
[Fact]
30-
public async Task CamelCaseDeserializeMatch()
29+
public async Task BuiltInPolicyDeserializeMatch()
3130
{
32-
var options = new JsonSerializerOptions();
33-
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
31+
// This is 1 because the data matches the property "MyInt16" using the specified policy.
32+
await DeserializeAndAssert(JsonNamingPolicy.CamelCase, @"{""myInt16"":1}", 1);
33+
await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"{""my_int16"":1}", 1);
34+
await DeserializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"{""MY_INT16"":1}", 1);
35+
await DeserializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"{""my-int16"":1}", 1);
36+
await DeserializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"{""MY-INT16"":1}", 1);
37+
}
3438

35-
SimpleTestClass obj = await Serializer.DeserializeWrapper<SimpleTestClass>(@"{""myInt16"":1}", options);
39+
private async Task DeserializeAndAssert(JsonNamingPolicy policy, string json, short expected)
40+
{
41+
var options = new JsonSerializerOptions { PropertyNamingPolicy = policy };
42+
var obj = await Serializer.DeserializeWrapper<SimpleTestClass>(json, options);
3643

37-
// This is 1 because the data matches the property "MyInt16" that is assuming camel-casing of "myInt16".
38-
Assert.Equal(1, obj.MyInt16);
44+
Assert.Equal(expected, obj.MyInt16);
3945
}
4046

4147
[Fact]
42-
public async Task CamelCaseSerialize()
48+
public async Task BuiltInPolicySerialize()
4349
{
44-
var options = new JsonSerializerOptions();
45-
options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
50+
await SerializeAndAssert(JsonNamingPolicy.CamelCase, @"""myInt16"":0", @"""myInt32"":0");
51+
await SerializeAndAssert(JsonNamingPolicy.SnakeCaseLower, @"""my_int16"":0", @"""my_int32"":0");
52+
await SerializeAndAssert(JsonNamingPolicy.SnakeCaseUpper, @"""MY_INT16"":0", @"""MY_INT32"":0");
53+
await SerializeAndAssert(JsonNamingPolicy.KebabCaseLower, @"""my-int16"":0", @"""my-int32"":0");
54+
await SerializeAndAssert(JsonNamingPolicy.KebabCaseUpper, @"""MY-INT16"":0", @"""MY-INT32"":0");
4655

47-
SimpleTestClass obj = await Serializer.DeserializeWrapper<SimpleTestClass>(@"{}", options);
56+
async Task SerializeAndAssert(JsonNamingPolicy policy, string myInt16, string myInt32)
57+
{
58+
var options = new JsonSerializerOptions { PropertyNamingPolicy = policy };
59+
var obj = await Serializer.DeserializeWrapper<SimpleTestClass>(@"{}", options);
4860

49-
string json = await Serializer.SerializeWrapper(obj, options);
50-
Assert.Contains(@"""myInt16"":0", json);
51-
Assert.Contains(@"""myInt32"":0", json);
61+
string json = await Serializer.SerializeWrapper(obj, options);
62+
63+
Assert.Contains(myInt16, json);
64+
Assert.Contains(myInt32, json);
65+
}
5266
}
5367

5468
[Fact]

0 commit comments

Comments
 (0)