-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Additional code coverage in System.Text.Json #33019
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some nits. But looks great.
@@ -0,0 +1,61 @@ | |||
using System.Collections.Generic; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a licence header comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 6c8ca54
yield return new object[] { "\"ABC===\"" }; | ||
yield return new object[] { "\"ABC\"" }; | ||
yield return new object[] { "\"ABC!\"" }; | ||
yield return new object[] { GenerateRandomInvalidLargeString(true) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: use named parameter to help with readability
yield return new object[] { GenerateRandomInvalidLargeString(true) }; | |
yield return new object[] { GenerateRandomInvalidLargeString(includeEscapedCharacter: true) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in e4ba4ce
private static string GenerateRandomValidLargeString() | ||
{ | ||
var random = new Random(42); | ||
var charArray = new char[502]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I know you copied this from existing tests, but why did we pick 502 as the size? Consider adding a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added explanation in b9b42e1
var random = new Random(42); | ||
var charArray = new char[502]; | ||
charArray[0] = '"'; | ||
for (int i = 1; i < charArray.Length; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are going to write "
at the end anyway, reduce index by one?
for (int i = 1; i < charArray.Length; i++) | |
for (int i = 1; i < charArray.Length - 1; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in d09874d
[InlineData(@"{""$iz"": ""1""}")] | ||
[InlineData(@"{""$rez"": ""1""}")] | ||
[InlineData(@"{""$valuez"": []}")] | ||
public static void InvalidMetadataPropertyNameWithSameLengthIsNotRecognized(string json) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
[InlineData(@"{""$valuez"": []}")] | ||
public static void InvalidMetadataPropertyNameWithSameLengthIsNotRecognized(string json) | ||
{ | ||
JsonException ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Employee>(json, s_deserializerOptionsPreserve)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to assert anything about the returned JsonException
itself and its properties?
If not:
JsonException ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Employee>(json, s_deserializerOptionsPreserve)); | |
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Employee>(json, s_deserializerOptionsPreserve)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point -- added validation of the expected path in 9c0af54
[InlineData(2, 3)] | ||
[InlineData(10, 11)] | ||
[InlineData(70, 71)] | ||
[InlineData(10, 0)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add a comment here that 0 (or default) max depth is treated as 64. Something like this (feel free to reword):
[InlineData(10, 0)] | |
[InlineData(10, 0)] // 0 (or default) max depth is treated as 64. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 70835c4
[InlineData(2, 2, 1)] | ||
[InlineData(10, 10, 9)] | ||
[InlineData(70, 70, 69)] | ||
[InlineData(70, 0, 63)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same nit: comment that 0 is special and means 64.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 70835c4
@ahsonkhan I found that there was another set of tests for Base64 strings using the exact same test data. I thought it would be a good idea to reuse it to reduce LoC, similar to other data sets like |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Part of #32341
Note 1
In order to get coverage on the following lines, I added a missing test case using
JsonDocument.Parse
with a large, invalid base64 string without an escaped character. The test data previously used[InlineData(...)]
attributes where the large strings were generated from the[InlineData(null)]
case, usingnull
as a sentinel value. I weighed a few different options for how to add this new case, and eventually settled on moving the test data to its own class and using[MemberData(...)]
instead to simplify the tests themselves. Certainly open to suggestions!runtime/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs
Lines 162 to 166 in 11b37df
Note 2
There was a missing case in cyclic tests that validates that
EffectiveMaxDepth
is set to 64 whenMaxDepth
is set to 0 (lines). I ended up splittingWriteCyclicFail
, which had two logical subgroups intoWriteCyclic
andWriteCyclicFail
.runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
Line 203 in 527adf2