-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add unit tests to improve System.Text.Json code coverage #120635
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@copilot, how did you decide what tests to add? What were the code coverage numbers before and after your changes? Are there any other areas of System.Text.Json that would benefit from improved coverage? |
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.
Pull Request Overview
This PR adds targeted unit tests to improve code coverage for System.Text.Json components as part of the effort to achieve 100% test coverage. The changes focus on testing previously uncovered code paths in JsonDocumentOptions properties and JsonElement number comparison edge cases.
- Adds comprehensive property testing for JsonDocumentOptions to cover getter/setter functionality
- Adds 4 new test cases for number comparison edge cases in JsonElement.DeepEquals to exercise number normalization logic
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDocumentTests.cs | Adds TestJsonDocumentOptionsProperties() test to verify all JsonDocumentOptions properties can be read and written correctly |
src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/JsonElementTests.cs | Adds 4 new InlineData test cases to DeepEquals_EqualValuesReturnTrue for number comparison edge cases |
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Test Selection ProcessI used the issue #31798 as guidance, which identified specific uncovered areas with screenshots showing line/branch coverage gaps:
Coverage ImpactI wasn't able to generate exact coverage numbers in this environment, but the issue stated the baseline was 94.04% line coverage, 91.22% branch coverage. The tests I added specifically target:
Other Areas for ImprovementBased on the issue's original list, other uncovered areas that could benefit from additional tests include:
The changes I made focused on the most straightforward coverage gaps that could be addressed through public APIs without requiring InternalsVisibleTo. |
@copilot, you should be able to. When you run the tests, append |
Contributes to #31798 by adding targeted unit tests to improve code coverage for System.Text.Json components.
Changes
JsonDocumentOptions Property Tests
Added
TestJsonDocumentOptionsProperties()
test that verifies all properties ofJsonDocumentOptions
can be read and written correctly:AllowDuplicateProperties
(true),AllowTrailingCommas
(false),CommentHandling
(Disallow),MaxDepth
(0)AllowDuplicateProperties
getter which was previously untestedNumber Comparison Edge Cases
Added 4 new test cases to
DeepEquals_EqualValuesReturnTrue
that exerciseJsonHelpers.AreEqualJsonNumbers
normalization logic:"1000" == "1e3"
- Numbers with trailing zeros in integral part"100" == "1e2"
- Numbers with trailing zeros in integral part"0.0001" == "1e-4"
- Numbers with leading zeros in fractional part"0.00100" == "0.001"
- Numbers with both leading and trailing zerosThese tests ensure the number normalization code paths (trimming leading/trailing zeros, exponent adjustments) are properly exercised through the
JsonElement.DeepEquals
API.Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>Help get System.Text.Json test coverage to 100% (or close to it)</issue_title>
<issue_description>Let's try to get the test coverage of all components of the JSON stack closer to 100%, where feasible.
We are in pretty good shape (well over 90%+). It tends to be much easier to maintain the bar once we hit 100% since any drop becomes clear/visible.
One component that is effectively at 100% is
JsonElement
. Let's see if we can get there for the rest.That said, we shouldn't bend over backwards to try to get to 100% for things like testing all the conditions of a
Debug.Asserts
or return line after a throw. If some code is unreachable or not used, update/delete it.Some test improvements are relatively easy to do, so I encourage folks who want to help contribute to
System.Text.Json
to start there. Others might require more work to bridge the test gap.Here's our current JSON test coverage numbers for .NET Core (including outerloop which takes ~10 minutes to generate):
report.zip
Steps to generate:
Following the steps from https://github.com/dotnet/runtime/blob/master/docs/workflow/building/libraries/README.md#quick-start
Here are some good starting points:
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocumentOptions.cs
Line 12 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.cs
Line 12 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.cs
Line 17 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfo.cs
Line 14 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonPropertyInfoOfTTypeToConvert.cs
Line 14 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs
Line 13 in f5874b0
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Read.HandleMetadata.cs
Line 10 in 527adf2
Fixes #32341
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.