Skip to content
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

Merged
merged 11 commits into from
Mar 4, 2020

Conversation

alanisaac
Copy link
Contributor

@alanisaac alanisaac commented Feb 29, 2020

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, using null 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!

if (pooledArray != null)
{
byteSpan.Clear();
ArrayPool<byte>.Shared.Return(pooledArray);
}

Note 2
There was a missing case in cyclic tests that validates that EffectiveMaxDepth is set to 64 when MaxDepth is set to 0 (lines). I ended up splitting WriteCyclicFail, which had two logical subgroups into WriteCyclic and WriteCyclicFail.

EffectiveMaxDepth = (value == 0 ? JsonReaderOptions.DefaultMaxDepth : value);

@layomia layomia added this to the 5.0 milestone Mar 2, 2020
@layomia layomia added the test-enhancement Improvements of test source code label Mar 2, 2020
Copy link
Member

@ahsonkhan ahsonkhan left a 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;
Copy link
Member

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.

Copy link
Contributor Author

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) };
Copy link
Member

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

Suggested change
yield return new object[] { GenerateRandomInvalidLargeString(true) };
yield return new object[] { GenerateRandomInvalidLargeString(includeEscapedCharacter: true) };

Copy link
Contributor Author

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];
Copy link
Member

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.

Copy link
Contributor Author

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++)
Copy link
Member

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?

Suggested change
for (int i = 1; i < charArray.Length; i++)
for (int i = 1; i < charArray.Length - 1; i++)

Copy link
Contributor Author

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)
Copy link
Member

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));
Copy link
Member

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:

Suggested change
JsonException ex = Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Employee>(json, s_deserializerOptionsPreserve));
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Employee>(json, s_deserializerOptionsPreserve));

Copy link
Contributor Author

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)]
Copy link
Member

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):

Suggested change
[InlineData(10, 0)]
[InlineData(10, 0)] // 0 (or default) max depth is treated as 64.

Copy link
Contributor Author

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)]
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 70835c4

@alanisaac
Copy link
Contributor Author

@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 ValidISO8601Tests. See 8c893c0

Copy link
Contributor

@layomia layomia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@layomia layomia merged commit cbd5166 into dotnet:master Mar 4, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json test-enhancement Improvements of test source code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants